I’ve got a table in which there are some columns with big text data. The query for 10 rows (table has only 31 records) takes more than 20 seconds. If I remove fields with big size, the query is executed quickly. The query for 1 row (by id) always executed quickly.
How can I do the query for many rows work more faster?
The query looks like this
SELECT DISTINCT (a.id), a.field_1, a.field_2, a.field_3
, a.field_4, a.field_5, a.filed_6, ...
FROM table_a a, table_b b
WHERE a.field_8 = 'o'
ORDER BY a.field_2 DESC
LIMIT 10;
@a_horse already hinted at the likely syntax error. Try:
Note the bold emphasis and read up on the DISTINCT clause in the manual.
Also, an index on
field_8might help.A multicolumn index on
(field_8, id, field_2)might help even more, if you can narrow it down to that (and if that is the sort order you want, which I doubt).If you want the result sorted by
a.field_2 DESCfirst:In PostgreSQL 9.1, if
idis the primary key: