Monday, September 19, 2011

MySQL Stored Procedures or How BTREE index improves performance on Temporary tables

Hi all, today i gonna tell you that i am sorry for poor english:)
I have met the next problem while using Stored Procedures.
The problem i had is stored procedure i've coded, after a bit of additional change fetched ~110k rows and stored all this info in a temporary table.
CREATE TEMPORARY TABLE tmp_revenues
SELECT SUM(commision)*provider.rv AS revenue, transaction.status
FROM transactions
LEFT JOIN provider ON (provider.name=transactions.provider)
AND transaction.date BETWEE date_start AND date_end
ORDER BY transactions.status
and a few more temp tables that stored information how to count each channel, provider and first counted providers then channels, so when i drove in trouble my Stored Procedure begun executed for about 3 minutes o.O.
Yes, i sat like O.O
I decided break such and joins into a simple queries (NOTE: That was not only query) and so on, i've done it like
after creating temporary tables ->
CREATE INDEX ix_tmp_rev ON tmp_revenues USING BTREE (status, provider, channel, date)
as those fields was necessary for me.
how does this work....
Temporary tables are created with ENGINE=MEMORY by default, so you should specify index type (USING BTREE) as BTREE default on ENGINE=MyISAM, but not MEMORY.

The only index types MEMORY supports is HASH by default and BTREE.
HASH is hashing values of any length and then quickly looking up, this type of index is bad for range comparisons. even impossible, cause MySQL will scan whole table.
BTREE indexing not hashed, so allowing us to apply range comparison. In my Case i used it as i have a range comparison by DATE field.
So we done CREATE INDEX index_name ON table_name USING INDEX_TYPE (field_names to index)
this little trick of breaking complicated queries into simple and implementing proper index did reduce my statistics Stored Procedure to execution time of 2-4secs depends on SQL Server load at the moment of execution.

I also removed few duplicated indexes on TRANSACTIONS table, DB designer with no experience did...
That also boosted performance!

Have fun with it.
Proper indexing have a lot of benefits.
 
 

No comments: