简体   繁体   中英

MySql, SELECT * FROM with Indexed columns

I'm working with a table in MySql that has an int indexed column called "serial". This table has around 2 million rows.

If I apply a select statement with this column in this way:

 SELECT serial FROM Table WHERE Serial=12345

this returns the data in around < 1 sec.

However, if I use a SELECT * query in this same table, this query takes around 78 seconds ...

I know it is not useful to apply indexes to all the columns in the table, how can I optimize/minimize the query response time if I need to get all the columns from a specific serial?

 SELECT * FROM Table WHERE serial= 12345

The results from EXPLAIN :

SELECT serial:

1 SIMPLE tableName index idx_tablename_serial 5 6686620 Using index

SELECT * :

1 SIMPLE agsensepivotreadings ALL (null values) 6686620

Please, any sugggestion or guide will be very appreciated.

Even limiting how many columns you need to read by a few will help. Just limit it even more and IF indexing a few more columns helps then go ahead but they'd need to be used in the WHERE clause.

This is too long for a comment.

It is rather unlikely that the columns are causing the problem. This could happen if one (or more) of the columns are really, really large objects. To get to 78 seconds, you need to be thinking in terms of many megabytes or gigabytes, although even 1 Gbyte might not take that long in many environments.

The use of the index versus the non-index is easy to explain. The first query is covered by the index, so the original data pages are not needed. The second query is not covered by the index. Because so many rows are being selected, all the data may need to be read, in order to find a matching row. This is an optimization to prevent thrashing. It might explain what is going on, although 78 seconds for loading a table into memory seems like a long time -- unless the rows are very wide.

Another possibility is that other operations are locking the table. In many environments, this would be the most likely culprit.

Finally, if the queries were subtly different (such as one having an order by or the constant being enclosed in single quotes), then that might account for some difference.

I would check the explain to see what is happening. Even searching through a table with a few million rows should not take 78 seconds.

There is part answer for your question.

https://stackoverflow.com/a/3211164/2957840

But also, maybe you should consider partitioning your table: https://dev.mysql.com/doc/refman/5.7/en/partitioning.html

If Serial is VARCHAR , you have a typing problem. Put quotes around "123456".

PARTITION will not help that query.

Please provide SHOW CREATE TABLE if you need to discuss this further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM