Thursday, January 16, 2014

SQL EXPLAIN to analyse and improve SQL queries

Database SQL query is slow?

You can use MySQL's EXPLAIN to analyse the query, maybe the column of the WHERE clause needs to be indexed?

EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,
               tt.ProjectReference, tt.EstimatedShipDate,
               tt.ActualShipDate, tt.ClientID,
               tt.ServiceCodes, tt.RepetitiveID,
               tt.CurrentProcess, tt.CurrentDPPerson,
               tt.RecordVolume, tt.DPPrinted, et.COUNTRY,
               et_1.COUNTRY, do.CUSTNAME
        FROM tt, et, et AS et_1, do
        WHERE tt.SubmitTime IS NULL
          AND tt.ActualPC = et.EMPLOYID
          AND tt.AssignedPC = et_1.EMPLOYID
          AND tt.ClientID = do.CUSTNMBR;

http://dev.mysql.com/doc/refman/5.0/en/explain-output.html


mysql> alter table MY_TABLE add INDEX IDX_F1_F2 (F1,F2);
mysql> SHOW INDEX FROM MY_TABLE;

http://devzone.advantagedatabase.com/dz/webhelp/advantage9.1/how_indexes_are_used_by_the_where_clause.htm

http://www.experts-exchange.com/Database/MySQL/A_1250-3-Ways-to-Speed-Up-MySQL.html 

No comments: