
*****
## 單表優化
建表
```
create table article(
id int unsigned not null primary key auto_increment,
author_id int unsigned not null,
category_id int unsigned not null,
views int unsigned not null,
comments int unsigned not null,
title varchar(255) not null,
content text not null
);
```
插入數據
```
insert into article(`author_id`,`category_id`,`views`,`comments`,`title`,`content`) values
(1,1,1,1,'1','1'),
(2,2,2,2,'2','2'),
(1,1,3,3,'3','3');
```
查詢category_id為1且comments大于1的情況下,views最多的article_id
## 雙表優化
建表
~~~
商品類別
create table class(
id int unsigned not null primary key auto_increment,
card int unsigned not null
);
圖書表
create table book(
bookid int unsigned not null auto_increment primary key,
card int unsigned not null
);
~~~
驅動表的概念,mysql中指定了連接條件時,滿足查詢條件的記錄行數少的表為驅動表;如未指定查詢條件,則掃描行數少的為驅動表。mysql優化器就是這么粗暴以小表驅動大表的方式來決定執行順序的。
- 1-數據庫-基本使用
- 1-1-數據存儲
- 1-2-數據庫
- 1-3-MySQL安裝和配置
- 1-4-SQL
- 1-5-數據完整性
- 1-6-命令行操作數據庫
- 2-MySQL查詢
- 2-1-MySQL查詢
- 2-2-條件
- 2-3-聚合函數
- 2-4-分組
- 2-5-排序
- 2-6-分頁
- 2-7-連接查詢
- 2-8-子查詢
- 2-9-自關聯
- 3-MySQL外鍵
- 4-MySQL與Python交互
- 4-1-數據準備
- 4-2-數據表的拆分
- 4-3-Python操作MySQL
- 5-MySQL高級
- 5-1-視圖
- 5-2-事務
- 5-3-索引
- 5-4-賬戶管理(了解)
- 6-數據庫存儲引擎
- 6-1-MyISAM存儲引擎
- 6-2-Innodb存儲引擎
- 6-3-CSV存儲引擎
- 6-4-Memory存儲引
- 7-MySQL基準測試
- 8-explain分析SQL語句
- 8-1-影響服務器性能的幾個方面
- 8-2-explain分析SQL
- 9-索引優化案例
- 10-索引優化
- 11-排序優化
- 12-慢查詢日志
- 13-Show Profile進行SQL分析
- 14-數據庫鎖
- 15-主從復制
- 16-MySQL分區表
- 17-MySQL操作規范