MYSQL这个如何优化

WBOY
Release: 2016-06-13 11:54:20
Original
671 people have browsed it

MYSQL这个怎么优化
SELECT * 
FROM add
ORDER BY LENGTH( title ) DESC 
LIMIT 10

数据20W条,执行要1秒多。。。。怎么优化哇 
------解决方案--------------------
这已经是最简单的了啊 还怎么优化啊 建议用缓存吧 查询的结果缓存起来 不需要反复查询了
------解决方案--------------------
order by 恐怕优化不了,1秒多也不慢呀。
------解决方案--------------------
20W条不多,建议重新设计数据库,新建一个列,储存LENGTH( title ) ,在插入时,用 strlen($title) 计算。然后这个新的列建立索引。(如果id是自增的话,可以尝试把 id和这个新列 作为一个primary key)
另外,不要用SELECT * ,用 SELECT title,content,date 之类的,把你需要的列写出来。
------解决方案--------------------
做一个 LENGTH( title ) 的索引
你的排序规则很怪异哟
------解决方案--------------------
desc SELECT * 
FROM add
ORDER BY LENGTH( title ) DESC 
LIMIT 10

分析下sql,关键字段加上索引。
------解决方案--------------------
关键字加索引能提升不少效率。
------解决方案--------------------
select * 可以不用返回 全部字段吗? 可以在存储的时候多一个字段来存储 LENGTH( title ) 的值 ,并且用两个触发器维护这个值(update和insert)后都要维护,再在这列上建立索引 ,应该会好一点吧

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!