Greg Stark napsal(a):
Actually you could try the equivalent query: SELECT Key FROM MRTPContactValue GROUP BY KeyThis may or may not be faster because it can use a hash aggregate plan. I would expect it to be faster here because there are few distinct keys and the planner predicts that. Eventually these two queries should be handled the same by Postgres but Hash Aggregates are a new addition and DISTINCT/DISTINCT ON hasn't been adapted to make use of them. Also, incidentally, I don't see how a table of possible keys could help you here. Nothing forces they table MRTPContactValue to use all possible keys... I simpified the case because it was slow by itself. GROUP BY really makes this a lot faster. The table contains properties for each contact that I cannot control how many properties and what names of the properties there will be. In my scenario user can export the data through user interface and I need to know what keys are used there to create appropriate column names. There is even one constraint. The contacts are grouped into groups so I need to get only the keys from a selected group. The real query is this (which is not so fast as the plain SELECT ... GROUP BY ... because the other table is also large enough) but now it is faster than before: SELECT Key FROM MRTPContactValue INNER JOIN MRTPContact ON MRTPContactValue.MRTPContactId = MRTPContact.Id WHERE MRTPContact.MRTPWaveQuestionnaireId = 1 GROUP BY Key Here's the query plan:
The same query using DISTINCT takes about 40 sec to complete. Thank you. -- Miroslav Šulc |
begin:vcard fn;quoted-printable:Miroslav =C5=A0ulc n;quoted-printable:=C5=A0ulc;Miroslav org:StartNet s.r.o. adr;quoted-printable;quoted-printable:;;Schodov=C3=A1 309/10;Praha 5;;150 00;=C4=8Cesk=C3=A1 republika email;internet:[EMAIL PROTECTED] tel;cell:+420 603 711 413 x-mozilla-html:TRUE url:http://www.startnet.cz version:2.1 end:vcard
---------------------------(end of broadcast)--------------------------- TIP 4: Have you searched our list archives? http://archives.postgresql.org