什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!
什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!
分页是一个很常见功能,select ** from tableName limit ($page - 1 ) * $n ,$n
通过一个存储过程插入10000条数据进行测试:
create table smth1 (
id int auto_increment ,
ver int(11) default null,
content varchar(1000) not null,
intro varchar(1000) not null,
primary key(id),
key idver(id,ver)
)engine = innodb default charset = utf8;
create procedure smthTest1()
begin
declare num int default 100001;
while num set num := num 1;
insert into smth1 values (num ,num,'我是*****','我是谁');
end while ;
end;
查询:
mysql> show profiles;
---------- ------------ ----------------------------------------------
| Query_ID | Duration | Query |
---------- ------------ ----------------------------------------------
| 1 | 0.002006 | select id ,content from smth1 limit 1000,10 |
| 2 | 0.030106 | select id ,content from smth1 limit 5000,10 |
| 3 | 0.042428 | select id ,content from smth1 limit 9000,10 |
| 4 | 0.01297225 | select id ,content from smth1 limit 10000,10 |
| 5 | 0.13077625 | select id ,content from smth1 limit 20000,10 |
可见随着查询$page 变大,时间会越来越大!
怎样避免这种情况?
一般我们数据库里面数据都不会直接删除,数据时很宝贵的,不舍得删除,另一方便能提高查询数据
先利用索引查询出来数据,再进行联合查询不就行了
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 1000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id ;
进行执行计划分析,没有一个大于1s的
11 | 0.04538625 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id |
| 12 | 0.023278 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id |
| 13 | 0.02320425 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id |
| 14 | 0.001938 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id |
此外,还会想到用in来查询而不是子查询,为什么不用in,,使用in会先查询出来一条id,之后再去和下面进行匹配,会进行smth1进行全表扫描!
--------------------------------------分割线 --------------------------------------
Ubuntu 14.04下安装MySQL
《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF
Ubuntu 14.04 LTS 安装 LNMP NginxPHP5 (PHP-FPM)MySQL
Ubuntu 14.04下搭建MySQL主从服务器
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
MySQL-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
本文永久更新链接地址:

MySQL使用的是GPL许可证。1)GPL许可证允许自由使用、修改和分发MySQL,但修改后的分发需遵循GPL。2)商业许可证可避免公开修改,适合需要保密的商业应用。

选择InnoDB而不是MyISAM的情况包括:1)需要事务支持,2)高并发环境,3)需要高数据一致性;反之,选择MyISAM的情况包括:1)主要是读操作,2)不需要事务支持。InnoDB适合需要高数据一致性和事务处理的应用,如电商平台,而MyISAM适合读密集型且无需事务的应用,如博客系统。

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

MySQL中有四种主要的索引类型:B-Tree索引、哈希索引、全文索引和空间索引。1.B-Tree索引适用于范围查询、排序和分组,适合在employees表的name列上创建。2.哈希索引适用于等值查询,适合在MEMORY存储引擎的hash_table表的id列上创建。3.全文索引用于文本搜索,适合在articles表的content列上创建。4.空间索引用于地理空间查询,适合在locations表的geom列上创建。

toCreateAnIndexinMysql,usethecReateIndexStatement.1)forasingLecolumn,使用“ createIndexIdx_lastNameEnemployees(lastName); 2)foracompositeIndex,使用“ createIndexIndexIndexIndexIndexDx_nameOmplayees(lastName,firstName,firstName);” 3)forauniqe instex,creationexexexexex,

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器