學習來源: codedata - MySQL 超新手入門(20)效率
CREATE TABLE test (
...,
address VARCHAR(255)
)
CREATE INDEX addr_index ON test (address)
CREATE INDEX addrindex ON test ( address (6) )
SELECT COUNT(*), COUNT(DISTINCT address)
FROM test;
SELECT COUNT(*), COUNT( DISTINCT LEFT(address, 6) )
FROM test
SELECT * FROM country WHERE name LIKE 'ta%'
SELECT * FROM country WHERE name LIKE '%ta'
SELECT * FROM country WHERE name LIKE '_ta%'
CREATE TABLE countrylanguage (
CountryCode char(3) NOT NULL DEFAULT '',
Language char(30) NOT NULL DEFAULT '',
...,
PRIMARY KEY (CountryCode, Language)
)
ALL
: 表示這個查詢發生「full table scan」,資料庫在這個表格從第一筆讀到最後一筆,才可以判斷要傳回哪些資料const
: 表示只讀取一筆資料,在條件中使用 主牽引或 唯一牽引index
:NULL
: 沒有使用牽引key名稱
: 使用牽引的名稱DELETE FROM ocuntry WHERE Code = 'YES'
EXPLAIN
SELECT * FROM country WHERE Code = 'YES'
INSERT INTO cmdev.emp VALUES
(8001, 'SIMON', 'MANAGER', 7369, '2001-02-03', 3300, NULL, 50),
(8002, 'Kelly', 'RNGINEER', 7370, '2003-02-03', 2300, NULL, 50),
(8003, 'JACK', 'PROGRAMMER', 7381, '2002-02-03', 2000, NULL, 50)
SELECT * FROM country LIMIT 100, 5
SELECT * FROM country ORDER BY Code LIMIT 5
CREATE TABLE countrycapital
SELECT Continent, co.Code, co.Name COuntryName,
ci.Name CaptitalName, ci.Population CaptitalPop
FROM co.Captial = ci.ID
WHERE co.Captial = ci.ID
ORDER BY Continent, CountryName
SELECT *
FROM countrycapital
WHERE CapitalPop < 5000
SELECT cc.*, cola.Language OfficalLanguage
FROM countrycapital cc, countrylanguage cola
WHERE cc.Code = cola.CountryCode AND
CapitalPop < 5000 AND IsOfficial = 'T'