Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing affects only the WHERE clause?

If I have something like:

CREATE INDEX   idx_myTable_field_x
ON             myTable
USING          btree (field_x);

SELECT COUNT(field_x), field_x FROM myTable GROUP BY field_x ORDER BY field_x;

Imagine myTable with around 500,000 rows and most of field_x values being unique.

Since I don't use any WHERE clause, will the created index have any effect at all in my query?

Edit: I'm asking this question because I don't get any relevant difference between query-times before and after creating the index; They always take about 8 seconds (which, of course is too much time!). Is this behaviour expected?

like image 573
acm Avatar asked Dec 03 '25 08:12

acm


2 Answers

The index will not help here as you are reading the whole table anyway there is no use in going to an index first (PostgreSQL does not yet have index-only scans)

Because nearly all values in the index are unique, it wouldn't really help in this situation anyway. Index lookups (including index-scans for other DBMS) tend to be really helpful for lookup of a small number of rows.

There is a slight possibility that the index might be used for ordering but I doubt that.

If you look at the output of EXPLAIN ANALYZE VERBOSE you can see if the sorting is done in memory or (due to the size of the result) is done on disk.

If sorting is done on disk, you can speed up the query by increasing the work_mem - either globally or just for your session.

Since field_x is the only column referenced in your query, your index covers the query and should help you avoid lookups into actual rows of myTable.

EDIT: As indicated in the comment discussion below, while this answer is valid for most RDBMS implementations, it does not apply to postgresql.

like image 27
Joe Stefanelli Avatar answered Dec 06 '25 01:12

Joe Stefanelli