简体   繁体   English

SQL 查询性能慢 'LIKE CNTR%04052021%

[英]SQL query slow performance 'LIKE CNTR%04052021%

we have a database that is growing every day.我们有一个每天都在增长的数据库。 roughly 40M records as of today.截至今天,大约有 4000 万条记录。

This table/database is located in Azure.该表/数据库位于 Azure 中。

The table has a primary key 'ClassifierID', and the query is running on this primary key.该表有一个主键“ClassifierID”,查询正在这个主键上运行。

The primary key is in the format of ID + timestamp (mmddyyy HHMMSS), for example 'CNTR00220200 04052021 073000'主键的格式为ID+时间戳(mmddyyy HHMMSS),例如'CNTR00220200 04052021 073000'

Here is the query to get all the IDs by date这是按日期获取所有 ID 的查询

 **Select distinct ScanID
 From ClassifierResults
 Where ClassifierID LIKE 'CNTR%04052020%**

Very simple and straightforward, but it sometimes takes over a min to complete.非常简单明了,但有时需要一分钟才能完成。 Do you have any suggestion how we can optimize the query?您对我们如何优化查询有什么建议吗? Thanks much.非常感谢。

The best thing here would be to fix your design so that a) you are not storing the ID and timestamp in the same text field, and b) you are storing the timestamp in a proper date/timestamp column.最好的办法是修复您的设计,以便 a) 您不会将 ID 和时间戳存储在同一个文本字段中,并且 b) 您将时间戳存储在正确的日期/时间戳列中。 Using your single point of data, I would suggest the following table design:使用您的单点数据,我建议以下表格设计:

ID           | timestamp
CNTR00220200 | timestamp '2021-04-05 07:30:00'

Then, create an index on (ID, timestamp) , and use this query:然后,在(ID, timestamp)上创建一个索引,并使用以下查询:

SELECT *
FROM yourTable
WHERE ID LIKE 'CNTR%' AND
      timestamp >= '2021-04-05' AND timestamp < '2021-04-06';

The above query searches for records having an ID starting with CNTR and falling exactly on the date 2021-04-05 .上面的查询搜索IDCNTR开头并恰好落在日期2021-04-05的记录。 Your SQL database should be able to use the composite index I suggested above on this query.您的 SQL 数据库应该能够使用我上面在此查询中建议的复合索引。

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

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