![]() |
|
|||||||
| Register | FAQ | Members List | Downloads | Calendar | Search | Today's Posts | Mark Forums Read | Webmaster Resources | Webmaster Blogs |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
You can learn a lot of about how mysql is going to execute a select query by using Explain select statement. You can use the information thus obtained to see what you can do to help mysql run this query faster. An Explain select statement returns information in the following fields :
1) id : Gives the actual numeric position of the sselect within the complete query statement 2) select_type : Displays the type of select query. 3) table : name of the table from which the rows are being retrived 4) type : the type of join which will be performed 5) possible_keys : Indexes available which can be used to select rows from the table 6) key : Display index which will be actually used find rows. 7) key_len : length of the key select for use by mysql 8) ref : Displays the constant or column against which the selected key will be compared with to retrieve rows 9) rows : Total numbers of rows which will be examined by mysql to successfully execute this query 10) extra : This column contains extra information regarding the query execution plan In my next post, i'll discuss some important fields among these. 6) |
|
|||
|
Quote:
Here's the SQL to create table and enter some sample data : Code:
create table students ( id int, name varchar(60), address varchar(240) ); insert into students values (1, 'john', 'jsd djs dkh'), (2, 'scott', 'kfdh fkhsd fks'), (3, 'nathan', 'fkhs kfjsf kfjsd'); [code] Select * from students where id = 2 [code] Lets run explain on this select to see how we can optimize it, the output is : Code:
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | students | ALL | NULL | NULL | NULL | NULL | 3 | Using where | +----+-------------+----------+------+---------------+------+---------+------+------+-------------+ using index, now imagine the time it would take if you had a table with million records. Since we are filtering records based on the value of id field, lets add an index on id field. Code:
alter table students add index(id); Code:
+----+-------------+----------+------+---------------+------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+---------------+------+---------+-------+------+-------------+ | 1 | SIMPLE | students | ref | id | id | 5 | const | 1 | Using where | +----+-------------+----------+------+---------------+------+---------+-------+------+-------------+ It might not seem very useful for optimization for simple queries, but explain statement can be very handy for optimizing complex queries with multiple table joins. |
![]() |
| Useful Resources & Sites |
| • Search Engine Marketing Company • UK Web Hosting • Build One Way Links |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
| UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons. |
| Subscribe to our feeds |