mysql数据库sql优化原则(经验总结)_MySQL
bitsCN.com 一、前提
这里的原则只是针对MySQL数据库,其他的数据库某些是殊途同归,某些还是存在差异。我总结的也是MySQL普遍的规则,对于某些特殊情况得特殊对待。在构造SQL语句的时候要养成良好的习惯。
二、原则总结原则1、仅列出需要查询的字段,这对速度不会明显的影响,主要是考虑节省应用程序服务器的内存。
原来语句: select * from admin
优化为: select admin_id,admin_name,admin_password from admin
原则2、尽量避免在列上做运算,这样导致索引失效。
原语句: select * from admin where year(admin_time)>2014
优化为: select * from admin where admin_time> '2014-01-01′
原则3、使用JOIN 时候,应该用小的结果驱动大的结果(left join 左边表结果尽量小 如果有条件应该放到左边先处理,right join 同理反向),同事尽量把牵涉到多表联合的查询拆分多个query(多个连表查询效率低,容易到之后锁表和阻塞)。
原来语句 select * from admin left join log on admin.admin_id = log.admin_id where log.admin_id>10
优化为: select * from (select * from admin where admin_id>10) T1 lef join log on T1.admin_id = log.admin_id
原则 4、注意LIKE 模糊查询的使用, 避免使用 %% ,可以使用 后面带% ,双%是不走索引的。
原来语句: select * from admin where admin_name like ‘%de%'
优化为: select * from admin where admin_name >='de' and admin_nam
原则5、使用批量插入节省交互 (当如如果使用存储过程来处理批量的sql 各种逻辑是更好的选择)。
原来语句:insert into admin(admin_name,admin_password) values (‘test1′,'pass1′);
insert into admin(admin_name,admin_password) values (‘test2′,'pass2′);
insert into admin(admin_name,admin_password) values (‘test3′,'pass3′)
优化为: insert into admin(admin_name,admin_password) values(‘test1′,'pass1′),(‘test2′,'pass2′),(‘test3′,'pass3′)
原则6、limit 的基数比较大时使用between。
原来语句:select * from admin order by admin_id limit 100000,10
优化为: select * from admin where admin_id between 100000 admin 100010 order by admin_id
原则7、不要使用rand函数获取多条随机记录。
原来语句: select * from admin order by rand() limit 20
优化为: select * from admin as t1 Join(select round(rand()*((select max(admin_id) from admin)-(select min(id) from admin))+(select min(id) from admin)) as id) as t2 where t1.id>=t2.id order by t1.id limit
原则8、避免使用NULL。
原则9. 不要使用 count(id) 使用 count(*)。
原则10、不要做无谓的排序操作,而应该使用索引完成排序。
三、总结数据库优化包括两个方面,一方面是SQL程序优化,另一方面是数据库配置的优化。另外在MySQL可以通过以下语句来查看帮助SQL进行优化:
set @@profiling = 1;
select * from typecho_comments order mail limit 10,30;
show profiles;
show profile for query

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...
