Download presentation
Presentation is loading. Please wait.
Published byVincent Carpenter Modified over 9 years ago
1
Introduction MySQL won't actually execute the query, just analyse it EXPLAIN helps us understand how and when MySQL will use indexes EXPLAIN returns a table of data from which you identify potential improvements
2
Introduction(2) Optimise queries in three ways – Modify or create indexes – Modify query structure
3
EXPLAIN – Worked Example 1 EXPLAIN SELECT * FROM candidate where id_career = 5 and salary > 100000 The three most important columns returned by EXPLAIN 1)Possible keys :All the possible indexes which MySQL could have used 2)Key : Chosen key 3)Rows: Rows scanned
4
EXPLAIN – Worked Example 1 (indexing 1) ALTER TABLE candidate ADD INDEX carr (id_career); ALTER TABLE candidate ADD INDEX sal (salary); EXPLAIN SELECT * FROM candidate where id_career = 5 and salary > 100000
5
EXPLAIN – Worked Example 1 (indexing 2) ALTER TABLE candidate ADD INDEX carr_sal (id_career, salary); EXPLAIN SELECT * FROM candidate where id_career = 5 and salary > 100000 The order in which fields were defined in a composite index affects whether it is available for use in a query – ALTER TABLE `candidate` DROP INDEX `carr`; – EXPLAIN SELECT * FROM candidate where id_career = 5
6
EXPLAIN – Example 2 EXPLAIN select * from candidate where id_career = 5 or salary= 7000000 Full table scan avoided – could also use UNION – EXPLAIN select * FROM candidate where id_career = 5 UNION select * FROM candidate where salary = 7000000
7
EXPLAIN – JOIN ● JOINing together large data sets (>= 100,000) is really where EXPLAIN becomes useful ● Each JOIN in a query gets its own row in EXPLAIN ● Make sure each JOIN condition is FAST ● Make sure each joined table is getting to its result set as quickly as possible
8
EXPLAIN – Example JOIN EXPLAIN SELECT b.name,a.name as 'job',b.salary FROM career a INNER JOIN candidate b ON b.id_career = a.id => need an index on b.id_career
9
The “extra” column With every EXPLAIN, you get an “extra” column, which shows additional operations invoked to get your result set. Some example “extra” values: – Using where – Using temporary table – Using filesort – Using index
10
Using filesort MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause. The keys then are sorted and the rows are retrieved in sorted order Avoid, because: – Doesn't use an index Involves a full scan of your result set Create tempory table on memory
11
Example Using FileSort EXPLAIN select * from candidate where id_career = 5 order by salary TABLE candidate ADD INDEX sal_car (salary,id_career); ALTER TABLE candidate ADD INDEX car_sal (id_career, salary);
12
Using index MySQL got your results just by consulting the index, – Which could well have been sat in memory MySQL didn't need to even look at the table to get you your results – Opening a table can be an expensive operation. MySQL can answer the next query more quickly Particularly useful in calculate data
13
Example Using Index EXPLAIN select avg(salary) from candidate where id_career = 5 Add index – ALTER TABLE candidate ADD INDEX car_sal (id_career, salary);
14
INSERT DELAYED Example – insert delayed into candidate (name,email) VALUES ('test insert ','abc@yahoo.com'); Very useful if you have clients that cannot or need not wait for the INSERT to completeINSERT The row will be queued to be inserted when the table is not in use by any other thread => The next query not wait until row is inserted The row in the queued will be written one block. => faster than performing many separate inserts. The queued rows are held only in memory until they are inserted into the table
15
UPDATE LOW_PRIORITY Example – UPDATE LOW_PRIORITY candidate_copy set name = 'Nguyen Van A_200000 test' where id = 200000 Execution of the UPDATE is delayed until no other clients are reading from the tableUPDATE Very useful if you have clients that cannot or need not wait for the UPDATE to complete UPDATE DEMO
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.