简体   繁体   English

比较列和同一列时查询性能的影响

[英]Query performance impact while comparing column with the same column

I really want to know about there would be any performance impact while comparing table column with the same table column like 我真的想知道在将表列与相同表列进行比较时会对性能产生任何影响

select id, name from person
where name = coalesce(NULLIF(:application_parameter,''),name)

So in above example if I don't recieve parameter from my application(if it's null or blank) then query will be 因此,在上面的示例中,如果我没有从应用程序中接收参数(如果它为null或空白),则查询将为

select id, name from person
where name = name

This is just a sample, but in my application it's really a complex query. 这只是一个示例,但是在我的应用程序中,这确实是一个复杂的查询。 I know there are some alternates to this like create dynamic query(append where condition only if you want it). 我知道有一些替代方法,例如创建动态查询(仅在需要时附加条件)。

But I am only interested to know what will be the performance impact if I compare column with the same column. 但是我只想知道如果将列与同一列进行比较会对性能产生什么影响

Yes. 是。 This version: 这个版本:

where name = coalesce(NULLIF(:application_parameter,''),name)

will not take advantage of an index on name . 不会利用name的索引。 The use of the function impedes index optimization. 该函数的使用会妨碍索引优化。

It is hard to get queries to both take parameters and to use indexes. 很难同时获取参数和使用索引。 You can try: 你可以试试:

where name = :application_parameter or :application_parameter is NULL

MySQL might optimize this correctly. MySQL可能会正确地优化它。 Otherwise this might do the trick: 否则,这可能会解决问题:

select id, name
from person
where :application_parameter is NULL
union all
select id, name
from person
where name = :application_parameter;

To see the performance impact in mysql you can look at the query after the optimizer had it. 要查看mysql中的性能影响,您可以在优化程序使用后查看查询。 Do a 做一个

EXPLAIN EXTENDED SELECT aCol FROM aTable WHERE aCol = aCol;

This will generate a WARNING with the result of the optimizer. 这将对优化器的结果产生警告。 You can get it by 你可以得到它

SHOW WARNINGS;

Here you will find: 在这里您会发现:

SELECT aCol FROM aTable WHERE 1;

So the condition gets optimized away :-) 因此条件得到优化:-)

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

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