How to control the order of a query regarding to inner join

informixjoin;performancequery-performance

I have 3 tables.

They have

table_a 5m records

table_b 20m records

table_c 20m records

I'm performing the query below.

SELECT * FROM table_a a 
INNER JOIN table_b b ON a.a_id = b.a_id AND b.b_come='100' 
INNER JOIN table_c c ON b.c_id = c.c_id AND c.c_type IN ('A','B')
WHERE date(a.a_dt) = '01/01/2012 00:00:00.000' 

The query is quite slow. I think it is because the data in its nature they have

about 1000 records of a.a_id = b.a_id

and

about 20m*20m records of b.c_id = c.c_id.

I think I should get the result of a.a_id = b.a_id then do the one of b.c_id = c.c_id so that the performance could be a lot better. But it seems INNER JOIN has no concept of order. How should I do this to improve the performance?

Best Answer

The easiest way to influence a query plan and make it run faster is to have good indexes and ensure the statistic on those indexes are up-to-date. In your case have indexes on the primary and foreign keys in each table. It would be best if you included b_come and c_type in their respecitive indeces, too. (Sorry, I'm not strong on Informix; if the syntax allows the include clause or filtered indexes investigate and implement these.)

Wrapping the date() function around a_dt generally precludes any use of an index on that column. If makes it "non-SARGable" in the jargon. It may be better to structure it as

WHERE a.a_dt >= '01/01/2012 00:00:00.000'
and   a.a_dt <  '01/02/2012 00:00:00.000' -- or whatever the day after 1/1 is in your locale.