简体   繁体   English

MySQL查询太慢

[英]MySQL query is too slow

at a MySQL database I have the following table: 在MySQL数据库中,我具有下表:

  • id int Primary key id int主键
  • timestamp timestamp 时间戳记
  • tpid varchar tpid varchar
  • tpidno int tpidno int
  • serialnumber int 序列号int
  • command varchar 命令varchar
  • sequence int 序列整数
  • startTime varchar startTime varchar
  • endTime varchar endTime varchar
  • PosData varchar PosData varchar
  • ... ...

I also have 3 secondary indices: 我还有3个二级索引:

  1. tpid,tpidno 正品,tpidno
  2. serialnumber 序列号
  3. command 命令

The table contains ~2.5M rows and it is about 500MB 该表包含约250万行,大约500MB

Although I have complex queries that work fast I have great delay on those two simple queries: 尽管我有快速运行的复杂查询,但在这两个简单查询上却有很大的延迟:

Select id, sequence, PosData 
  From myTable 
 Where  serialNumber = 130541  
   and command = "myCommand" 
   and startTime = "20140106194300" 
   and endtime = "20140106200000"

(~4.4sec) (〜4.4sec)

Select id 
  From myTable 
 Where  serialNumber = 130541  
   and command = 'myCommand' 
   and sequence = 128

(~4.5sec) (〜4.5sec)

Does more indices like 是否有更多的指数喜欢

  • serialnumber, command 序列号,命令
  • command, sequence 命令,顺序

or 要么

  • serialnumber,command, sequence 序列号,命令,顺序

will speed up the queries? 将加快查询速度?

At the first query is it possible the data type of startTime and endTime to be the problem? 在第一个查询中,startTime和endTime的数据类型是否可能成为问题? if they were int instead of varchar it would be better? 如果它们是int而不是varchar,那会更好吗?

any other suggestions? 还有其他建议吗?

A single index on (serialnumber, command) will definitively improve performance for those two queries. (serialnumber, command)上的单个索引将最终提高这两个查询的性能。 Further you could add the other columns to make it even faster. 此外,您可以添加其他列以使其更快。 However, the best choice of the other columns depends on the data distribution and on the question which of the two statements is more often executed. 但是,其他列的最佳选择取决于数据分布以及以下问题:两个语句中哪个更常执行。 It might even not worth adding those columns if the other two columns are very selective. 如果其他两列的选择性很高,那么甚至不值得添加这些列。

The datatype for startdate and enddate is unfortunate at least. 对于数据类型startdateenddate是不幸的,至少。 Proper types will improve performance in a range from "a little" to "a lot" depending on you SQL. 适当的类型将根据您的SQL在从“一点”到“很多”的范围内提高性能。 The SQL above will be in the "a little" range. 上面的SQL将在“少许”范围内。

Some refs: 一些参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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