
Luckydonald
16.06.2017
05:57:33

Святослав
16.06.2017
06:24:29

Matthew
16.06.2017
06:40:42
Yeah those were just basic example queries
It seems like it could be a good way of building up queries with a lot of nested ands and ors

Google

Matthew
16.06.2017
07:01:40
Dynamically
For example in a loop doing query = query | select(...)
Also with & and -

Henri
18.06.2017
18:34:19
If someone is interested in some insides of the Morepath web-framework, Martijn Faassen gave just a great talk at PyMunich.
https://www.youtube.com/watch?v=BUQhElQuxMI


stsouko
20.06.2017
19:30:19
Hello! Is it possible on pony write a subqueries?
currently I do this:
sql_select = "x.bit_array %s '%s'::int2[]" % (operator, bit_set)
sql_smlar = "smlar(x.bit_array, '%s'::int2[], 'N.i / (N.a + N.b - N.i)') as T" % bit_set
select((x.molecule.id, x.id, raw_sql(sql_smlar)) for x in MoleculeStructure if raw_sql(sql_select)).order_by(raw_sql('T DESC')).limit(number * 2)
but order operation is slow. and I want limiting it.
SELECT "x"."molecule", "x"."id", smlar(x.bit_array, '{1409, 2306, ......}'::int2[], 'N.i / (N.a + N.b - N.i)') as T
FROM "daniel"."molecule_structure" "x"
WHERE x.bit_array %% '{1409, 2306, ......}'::int2[]
ORDER BY T DESC
LIMIT 20
replace this by:
SELECT * FROM
(SELECT "x"."molecule", "x"."id", smlar(x.bit_array, '{1409, 2306, ......}'::int2[], 'N.i / (N.a + N.b - N.i)') as T
FROM "daniel"."molecule_structure" "x"
WHERE x.bit_array %% '{1409, 2306, ......}'::int2[]
LIMIT 20) as SQ
ORDER BY T DESC


Alexander
20.06.2017
19:34:56
Yes, it is possible to write the most popular types of subqueries with Pony. I'm not quite understand at this moment what type of subquery you want to use

stsouko
20.06.2017
19:36:28
this is similarity search on arrays. I use SMLAR extension.

Alberto
20.06.2017
20:15:12
Hi! I have a model with some Optional fields, but I noticed something, and I want to be sure it's Pony related and not the result of something I did.
Suppose I have a model with some required fields and also with X = Optional(date) and Y = Optional(str). Then I create an instance of the model, without providing values for X and Y
I noticed that in that case X is assigned a None value, but Y is stored as an empty string. Is that how Pony works?
Thanks!


Alexander
21.06.2017
00:01:05
Yes, this is how Pony and most other ORM works: https://docs.ponyorm.com/api_reference.html#optional-string-attributes
It is possible to use two different values to represent missing strings - empty string and null. It may be confusing if both empty string and null are used in the same column, because some queries may return confusing results. So it is better to use just a single way to represent missing string values, and disallow another way for consistency. It is easier to disallow null than to disallow empty string values. Also, empty strings can be indexed, whereas null values are not indexed.

Alberto
21.06.2017
01:18:34
Ahhh ok
It makes sense

Google

Alberto
21.06.2017
01:18:41
Thanks for the clarification!

Matthew
23.06.2017
01:08:01
Is JOIN() no longer in the documentation? I just spent 10 minutes looking on the site and googling but I can't find it?
I'm sure I'm just overlooking something

Alexey
23.06.2017
01:13:37

Matthew
23.06.2017
01:13:50
I'm trying to speed up a query like this:
select(r for r in R if r.p in u.ps)
u.ps is approx 3000 items, so there is a big IN part to the query, JOIN() doesn't seem to help, what can I do?
Table is approx 2 million rows, with 3000 ps, it takes 25 - 30 seconds
ah with inversing the IN and JOIN it gets a lot faster

Alexey
23.06.2017
01:36:12
try this way
select(r for r in R if exists(u for u in U if r.p == u.ps))
ah, wait, u.ps is a set

Matthew
23.06.2017
01:37:33
Yep :)
I think it's a database issue fundamentally
50% of execution time is being taken by filtering a double precision column for some reason
-> Seq Scan on review r (cost=0.00..148774.57 rows=1782971 width=474)
Filter: ((review_rating >= 1::double precision) AND (review_rating <= 5::double precision))
which means i probably need to adjust indexes

Alexey
23.06.2017
01:44:48
ok, I see

Henri
23.06.2017
13:01:38
Is it possible to make non persistent changes to a table?
E.g. creating an entity instance for the need of the current request without writing it to the database?
Something like
fortunes = Fortune.select()[:]
fortunes.append(Fortune(message='Additional fortune added at request time.'))
When the model looks like
class Fortune(db.Entity):
message = Optional(str)

Alexander
23.06.2017
13:17:55
If you want to pass the list to some frontend layer with some additional element, maybe you can convert objects to tuples or dictionaries. Something like that:
fortunes = Fortune.select()
fortune_list = [f.to_dict(["id", "message"]) for f in fortunes]
fortune_list.append({id: additional_id, message: additional_message})

Henri
23.06.2017
15:56:44
Thanks! That works for me.?

Andrey
23.06.2017
22:53:05
Hi! Maybe I have a strange question, but is it possible for an entity with a composite primary key to have a self-reference where one part is a common table? Like for the following situation:
CREATE TABLE regions (
file_id INTEGER NOT NULL REFERENCES files (id),
region_id INTEGER NOT NULL,
PRIMARY KEY (file_id, region_id),
outer_region_id INTEGER,
FOREIGN KEY (file_id, outer_region_id) REFERENCES regions (file_id, region_id),
total_lines INTEGER
);(outer_region must be in the same file)

Google

Andrey
23.06.2017
22:55:28
I've tried the following code, but received pony.orm.core.DBSchemaError: Column 'file_id' already exists in table 'regions':
class Region(db.Entity):
_table_ = "regions"
file = Required(File, column="file_id")
region_id = Required(int, column="region_id")
PrimaryKey(file, region_id)
outer_region = Optional("Region", reverse="inner_regions", columns=["file_id", "outer_region_id"])
inner_regions = Set("Region", reverse="outer_region")
total_lines = Optional(int)


Matthew
24.06.2017
15:34:27
2017-06-24 15:21:52 UTC LOG: duration: 3846.543 ms statement: UPDATE "reviewproduct"
SET "first_review_scrape" = false
WHERE "id" = 2301
AND "asin" = 'XXX'
AND "country_code" = 'US'
AND "first_review_scrape" = false
How come pony is adding those WHERE clauses, instead of just specifying the ID? It's the only cause I can think of for a simple UPDATE taking nearly 4 seconds?
this happens with:
product.first_review_scrape = False as the only relevant code
if I do:
if product.first_review_scrape:
product.first_review_scrape = False
Then that slow update isn't issued, is it by design for pony to issue an update even when the value stays the same (False in this case)?


Alexander
24.06.2017
16:09:26
Hi Matthew!
> How come pony is adding those WHERE clauses, instead of just specifying the ID?
Pony doing this in order to do optimistic updates. Concurrent transactions should be isolated from each other. There are two ways to achieve such isolation. The first one is to use locks. It may be slow, because if one transaction locked a row in a table, other transactions need to wait until that lock will be released in order to access that row. The second way is to use optimistic updates, this is what Pony tries to do. When current transaction read some attributes of an object, it remember the values of these attributes. If it is necessary to update some attributes of this object, Pony issues an UPDATE query with additional checks that the attributes which were read by application code still contain the same values. If another concurrent transaction already updated some of this attributes, the UPDATE statement will not work and Pony will know that two different transactions are trying to change the same object using a potentially inconsistent way to do it. In other words: if you application code read x.a and x.b and then decided to change x.c, but another concurrent transaction already changed x.a or x.b, then new value of x.c`may be wrong because it does not take into account updated value of `x.a or x.b
> It's the only cause I can think of for a simple UPDATE taking nearly 4 seconds?
I don't think that an additional check can slow down an UPDATE query. Typically, additional constraints can only speed up the query, because they give to the database an additional ways to optimise that query. Speaking about your query, I'm pretty sure that the database is smart enough to understand that id column is primary key and found row using access by id, and check another column when row is already found without any significant slowdown.
The only reason I can think why your UPDATE query may be slow is that some concurrent transaction have locked that row and then wait 4 seconds before commit. Typically if some transaction issue UPDATE query it should commit as fast as possible in order to release locks and make that row available to another transactions.
> is it by design for pony to issue an update even when the value stays the same
Yes, it is by design. Pony sees that the application code want to have that flag to be set to True after the transaction is completed. Even if that flag was set to True initially, it is possible that some concurrent optimistic transaction already set it to False. So if Pony does not issue UPDATE query, assuming that the flag value is already True, the end result may be wrong.


Matthew
24.06.2017
16:10:48
ok, I had guessed it was by design :)
the if statement fixes it, so it isn't a serious problem

Alexander
24.06.2017
16:12:45
It is still interesting why UPDATE query takes so long time (if it is really the UPDATE query which adds slowdown). Maybe concurrent transactions do something suboptimal
Do you use db_session with some additional flags?

Matthew
24.06.2017
16:13:43
It should be the only transaction using that row, as it is a background task set by a scheduler, so no double accesses
no, just plain db_session
I am spending the last evening of my vacation "fire fighting" postgres performance, it is a love / hate relationship :)
i am hoping the other problem is just a case of adding a better index

Alexander
24.06.2017
16:16:16
I wish you success with that

Matthew
24.06.2017
16:16:28
select((x.date, x.a) for x in X)
Is there a cheap way to get max one result per unique date, with the lowest "a" value? I'm currently doing a blind select, with no limit, but it is too slow

Alexander
24.06.2017
16:17:30
select((x.date, min(x.a)) for x in X)

Matthew
24.06.2017
16:23:24
Thank you! I was overcomplicating it
the problem was not solved by a new index or clever code, but by a manual VACUUM

Alexander
24.06.2017
22:55:45
You mean slow UPDATE query?

Matthew
24.06.2017
22:56:35
no, my other problem, that was related to min() query

Google

Matthew
24.06.2017
22:57:13
There had been no autovacuum for 12 days, the table has a lot of daily data churn

Alexander
24.06.2017
23:31:15
I think it should be possible to set up autovacuum

Matthew
24.06.2017
23:32:00
It is, and I have tweaked it before, but it didn't work, so a cronjob makes sure that it is happening regularly

Arturo
26.06.2017
15:17:26
Hi
I'm reading through the docs
Quick question
Is it possible to generate/define entities automatically?]

Matthew
26.06.2017
15:18:28
from what source of information?

Arturo
26.06.2017
15:18:36
from the database itself
based on its schema

Alexander
26.06.2017
15:20:33
Right now no, but we are working on it. Early prototype should be available before the end of this week.

Arturo
26.06.2017
15:21:26
Thanks!!
Amazing work :)

Juan
27.06.2017
03:01:36
I think that this project is amazing! Congratulations for everyone that help and comment errors. Thank you for the project

Alexey
27.06.2017
03:04:45
Arturo @juan_castano and everyone here - Thank you for your support!

Luckydonald
28.06.2017
08:40:17
Wooh!

Alexander
28.06.2017
11:11:20
Guys, I need to reset my telegram account (I forgot my Telegram cloud password, and it cannot be recovered). Hope my previous messages will not be deleted ?

Henri
28.06.2017
16:17:01
I have a model like
class World(db.Entity):
randomnumber = Optional(int)
When doing
id_ = randint(1, 10000)
return {'id': id_, 'randomNumber': self[id_].randomNumber}
I get the error
TypeError: 'World' object does not support indexing
Any idea what's going on?
BTW
self == World

Matthew
28.06.2017
16:18:06
is it an instance of World, or the class World?

Google

Alexander
28.06.2017
16:18:13
Replace self[id_] to self.__class__[id_]

Matthew
28.06.2017
16:18:57
and maybe World.select().random(1) would be better?

Alexander
28.06.2017
16:19:18
Probably so

Matthew
28.06.2017
16:21:25
Have you already created the 10,000 rows?

Henri
28.06.2017
16:24:07

Matthew
28.06.2017
16:24:53
What general problem are you trying to solve?

Henri
28.06.2017
16:26:04
Add Morepath framework with PonyORM to http://frameworkbenchmarks.readthedocs.io

Matthew
28.06.2017
16:27:00
So you need a reliable record of 10,000 ID -> random ints ?

Henri
28.06.2017
16:27:45
Yeah there is a SQL file I need to import I think.

Matthew
28.06.2017
16:30:41
import random
class World(db.Entity):
random_number = Optional(int)
@classmethod
def get_random_number(cls):
return cls.select().random()[0]
@classmethod
def create_random_numbers(cls):
for _ in range(10000):
cls(random_number=random.randrange(0, 100))
commit()
untested
assumes you want random numbers to be 0 - 99 in range

Henri
28.06.2017
20:33:20
BTW after importing the tables I get now a really strange error when trying the above (Partly translated from German):
pony.orm.core.TransactionIntegrityError: Object World[new:1] cannot be stored in the database.
IntegrityError: ERROR: NULL-value in row »id« hurts Not-Null-Constraint
DETAIL: failed line contains (null, 0).
No idea where this comes from as I'm not inserting anything.
OK this is fixed now! ?