Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql fix Using where;

My SQL Query:

SELECT *
FROM updates_cats
WHERE uid =118697835834
ORDER BY created_date ASC

Current Indexes:

index1(uid, created_date)

EXPLAIN EXTENDED result:

1 SIMPLE updates_cats ref index1 index1 8 const 2 100.00 Using where

How can i fix the Extra field where it has Using where so it can use the indexes instead?

EDIT: SHOW CREATE TABLE:

CREATE TABLE `updates_cats` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `u_cat_id` bigint(20) NOT NULL DEFAULT '0',
 `uid` bigint(20) NOT NULL,
 `u_cat_name` text COLLATE utf8_unicode_ci NOT NULL,
 `total_updates` int(11) unsigned NOT NULL DEFAULT '0',
 `created_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 PRIMARY KEY (`id`),
 KEY `index1` (`uid`,`created_date`)
) ENGINE=MyISAM AUTO_INCREMENT=23522 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
like image 789
stergosz Avatar asked Mar 02 '12 13:03

stergosz


People also ask

What is the use of WHERE clause in MySQL?

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

What is Key_len in MySQL explain?

The key_len column indicates the length of the key that MySQL decided to use. The value of key_len enables you to determine how many parts of a multiple-part key MySQL actually uses. If the key column says NULL , the key_len column also says NULL .

How do I force an index in MySQL?

Pre-requisites: You have to create a database table with data in a MySQL database to check the Force Index feature of MySQL. Open the terminal and connect with the MySQL server by executing the following command. Run the following command to create a database named test_db.

How does MySQL choose which index to use?

If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index). If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.


1 Answers

The only thing that would be better than Using where is Using where; Using index with a "covering index". Try selecting just uid and created_date.

Using where is fine. It means it's applying the indicated index to the WHERE clause and reducing the rows returned. To get rid of it, you'd have to get rid of the WHERE clause.

Here are things that you should be concerned about:

  • Using filesort
  • Using temporary
  • Not using an index: NULL in the 'key' column of the EXPLAIN and a large number of rows in the 'rows' column.

Your EXPLAIN result shows that MySQL is applying index1 to the WHERE clause and returning 2 rows:

1 SIMPLE updates_cats ref index1 index1 8 const 2 100.00 Using where
like image 95
Marcus Adams Avatar answered Oct 25 '22 05:10

Marcus Adams