Home > Database > Mysql Tutorial > mysql 查询重复的数据的SQL优化方案_MySQL

mysql 查询重复的数据的SQL优化方案_MySQL

WBOY
Release: 2016-06-01 13:04:46
Original
872 people have browsed it

在mysql中查询不区分大小写重复的数据,往往会用到子查询,并在子查询中使用upper函数来将条件转化为大写。如:

代码如下:


select * from staticcatalogue WHERE UPPER(Source) IN (SELECT UPPER(Source) FROM staticcatalogue GROUP BY UPPER(Source) having count(UPPER(Source))>1) ORDER BY upper(Source) DESC;

这条语句的执行效率是非常低的,特别是Source字段没有加索引。尤其是最忌讳的在查询条件中使用了函数,这将极大的降低查询速度,如果查询十万条数据以内的10分钟内还能获取到数据,如果是查询几十万条的话,会直接把服务器跑死的,此时可以通过一个临时表,并且加索引,再查询。这样可以提高很多的速度

代码如下:


CREATE TABLE staticcatalogue_tmp SELECT UPPER(Source) AS Source FROM staticcatalogue GROUP BY UPPER(Source) having count(UPPER(Source))>1;
ALTER TABLE staticcatalogue_tmp add INDEX TX_1 (Source);
select s.* from staticcatalogue s WHERE UPPER(s.Source) IN (SELECT st.Source FROM staticcatalogue_tmp st) ORDER BY UPPER(s.Source) DESC ;

以上就是本文sql优化方案的全部内容了,希望大家能够喜欢。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template