简体   繁体   English

mysql以更快的方式?

[英]Mysql in a faster way?

I have a table in MySQL database: 我在MySQL数据库中有一个表:

  CodeNo Block
   a1     a
   a2     a
   b1     b
   b2     b
   c1     c
   c2     c 

I can query by using one of the two alternatives: 我可以使用以下两种方法之一进行查询:

select codeno from mytab where block='b' and codeno like 'b%'

alternatively 或者

select codeno from mytab where codeno like 'b%'

Which one is faster in an actual scenario when there are millions of records in mytab? 当mytab中有数百万条记录时,在实际情况下哪一个更快? Also could anyone explain the way it is actually stored in the database? 还有谁能解释它实际存储在数据库中的方式?

I think both will take same time because execution plan for both is same 我认为两者都需要相同的时间,因为两者的执行计划是相同的

在此处输入图片说明

  1. Select data 选择数据

  2. Table scan 100% 表扫描100%

and also look execution plan details 并查看执行计划的详细信息

在此处输入图片说明

在此处输入图片说明

Without seeing the Execution plan i would guess that the first filter would reduce the amount of records on which the second filter is set, which makes the first option faster, specially that the like operator is a string comparision one ,which is not really as effective as numbers or binary comparison. 如果没有看到执行计划,我会猜测第一个过滤器会减少设置第二个过滤器的记录数量,这会使第一个选项更快,特别是like运算符是一个字符串比较器,实际上并没有那么有效作为数字或二进制比较。

To know more about this you can generate the execution plan of both statements and compare running times, take a look a the documentation 要了解更多信息,您可以生成两个语句的执行计划并比较运行时间,请查看文档

First query should be faster, it limits the result by using block filter. 第一个查询应该更快,它通过使用块过滤器来限制结果。

I would say: apply an index on block to increase speed. 我会说:在block上应用索引以提高速度。

ALTER TABLE mytab ADD INDEX blockindex(block); ALTER TABLE mytab添加索引blockindex(block);

see sqlfiddle and compare the execution plans: http://sqlfiddle.com/#!9/317d6/1 请参阅sqlfiddle并比较执行计划: http ://sqlfiddle.com/#!9/317d6/1

See @juergend comment about explain. 请参阅@juergend有关注释的评论。 The problem is "it depends". 问题是“取决于”。 If no indexes on either field, the DB will do full table scan so no real difference. 如果在任何一个字段上都没有索引,则数据库将执行全表扫描,因此没有真正的区别。 If codeno has index and block does not then the two statement would still be roughly equivalent since it would use the index on codeno in either case. 如果codeno具有index和block则没有,那么这两个语句仍然大致相等,因为在两种情况下都将使用codeno上的索引。 If index on both fields the one with two conditions COULD be faster if the DB decided it is more beneficial to use the block index for first access. 如果在两个字段上都具有索引,则如果DB认为使用块索引进行首次访问会更有利,那么具有两个条件的字段可能会更快。 But the explain should show you what the DB decided to use for access plan. 但是说明应该向您显示数据库决定用于访问计划的内容。

In case you do not have any indexes on the table, the first query will take slightly more time, it is because nothing can be taken from memory, but you have to check two fields instead of one. 如果表上没有任何索引,则第一个查询将花费更多时间,这是因为无法从内存中获取任何内容,但是您必须检查两个字段而不是一个字段。

But this kind of denormalization is indeed very useful when you can add your own indexes. 但是,当您可以添加自己的索引时,这种非规范化确实非常有用。 You can add just (block) index, so that the equality check would be done in memory, and the data for the second condition would be taken from the disk, but only for those rows that matched the first condition. 您可以仅添加(block)索引,以便在内存中进行相等性检查,第二条件的数据将从磁盘中获取,但仅针对与第一个条件匹配的那些行。 This is especially useful when most of the rows the match the first condition match the second as well. 当匹配第一个条件的大多数行也匹配第二个条件时,这特别有用。

From the other side, you can add (codeno(1)) index, and the index would be used for the prefix check like codeno like 'b%' (be sure to specify the long enough prefix in the index), and this would take almost the same time. 另一方面,您可以添加(codeno(1))索引,该索引将用于前缀检查,如codeno like 'b%' (确保在索引中指定足够长的前缀),这将几乎需要相同的时间。 In this case the condition block='b' is not required, and even impedes. 在这种情况下,甚至不需要条件block='b'

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

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