Do not always use the Select *

Click on the blue " Python space " Ah my attention

Add a " star " happy learning together every day

Author: AIOps

From: between the Java (the above mentioned id: gengecn)

Application slow as an ox, a lot of reasons, may be the cause of the network, system architecture may be the reason, there may be the cause of the database.

So how to improve database SQL statement execution speed? Some would say tuning a database administrator (DBA) to do, but performance tuning with programmers also have a great relationship.

Program embedded SQL statements line by line, if you use some optimization tips, will be able to achieve a multiplier effect.

Tip 1 comparison operators use "=" would not "<>"

"=" To increase the chance of using the index.

Tip 2 knowing that only one query results, then please use the "LIMIT 1"

"LIMIT 1" to avoid full table scan to find the corresponding results of the scan will not continue.

Tips 3 column select the appropriate data type

TINYINT SMALLINT do not use, do not use SMALLINT INT, you know the truth, disk and memory consumption as small as possible thing.

Tips 4 large DELETE, UPDATE or INSERT query becomes more small queries

You can write a few lines, hundreds of lines of SQL statements are not forced to appear very high grid? However, in order to achieve better performance and better data control, you can turn them into multiple small queries.

5 instead of using techniques UNION ALL UNION, allowing repeat if the result set it

UNION ALL because not re efficient than UNION.

Tip 6 multiple times to get the same result set, please keep the SQL statement is consistent

The aim is to make full use of the query cache.

For example, according to the geographical and product id Product Pricing, for the first time we used:

那么第二次同样的查询,请保持以上语句的一致性,比如不要将where语句里面的id和region位置调换顺序。

技巧7  尽量避免使用 “SELECT *”

如果不查询表中所有的列,尽量避免使用 SELECT *,因为它会进行全表扫描,不能有效利用索引,增大了数据库服务器的负担,以及它与应用程序客户端之间的网络IO开销。

技巧8  WHERE 子句里面的列尽量被索引

只是“尽量”哦,并不是说所有的列。因地制宜,根据实际情况进行调整,因为有时索引太多也会降低性能。

技巧9  JOIN 子句里面的列尽量被索引

同样只是“尽量”哦,并不是说所有的列。

技巧10  ORDER BY 的列尽量被索引

ORDER BY的列如果被索引,性能也会更好。

技巧11  使用 LIMIT 实现分页逻辑

不仅提高了性能,同时减少了不必要的数据库和应用间的网络传输。

技巧12  使用 EXPLAIN 关键字去查看执行计划

EXPLAIN 可以检查索引使用情况以及扫描的行。

其他

SQL调优方法有很多种,同样的查询结果可以有很多种不同的查询方式。其实最好的方法就是在开发环境中用最贴近真实的数据集和硬件环境进行测试,然后再发布到生产环境中。

-END-

推荐阅读:
速观!GitHub 总星 5.4w+,这里藏着 Git 从入门到轻松玩转的秘密!

出不了门的日子,我选择在 GitHub 上快乐的打游戏

神级宝库!GitHub 标星 1.2w+,Chrome 最天秀的插件都在这里啦!

全!全!全!GitHub 总星 5.7w+,最赞的操作系统软件都在这里啦!
B站收藏 6.1w+!GitHub 标星 3.9k+!这门神课拯救了我薄弱的计算机基础





????扫描上方二维码即可关注
发布了610 篇原创文章 · 获赞 6951 · 访问量 120万+

Guess you like

Origin blog.csdn.net/u013486414/article/details/104666456