MYSQL索引類型
按邏輯來分:
1.主鍵索引
是一種特殊的唯一索引,不允許有空值
創(chuàng)建、刪除語句:
alter table [table_name] add primary key (column_name)
create table [table_name](
id int not null,
primary key (id)
)
alter table drop primary key
2.普通索引(單例索引)
對表中一個列進行索引
create index index_name on [table_name] (column_name)
alter table [table_name] add index [index_name] (column_name)
alter table drop index [index_name]
3.復(fù)合索引(多列索引)
對表中多個列進行索引
alter table [table_name] add index [index_name]
(column_name1,column_name2)
4.全文索引
可以在char、varchar或text類型的列上創(chuàng)建。
alter table [table_name] add fulltext (column_name)
5.唯一索引
當前列中的數(shù)據(jù)具有唯一性
alter table [table_name] add unique [index_name] (column_name)
在實際操作過程中,應(yīng)該選取表中哪些字段作為索引?
1.選擇唯一性索引
2.為經(jīng)常需要排序、分組和聯(lián)合操作的字段建立索引
3.為常作為查詢條件的字段建立索引
4.限制索引的數(shù)目
5.盡量使用數(shù)據(jù)量少的索引
6.盡量使用前綴來索引
7.刪除不再使用或者很少使用的索引
視圖作用
測試表:user有id,name,age字段
測試表:product有id,name,price字段
測試表:user_product有id,uid,pid字段
提高了重用性,就像一個函數(shù)
比如我要獲取一張用戶表和一張商品表的中用戶購買了那個商品的信息
select * from user as u , products as p, user_product as c where u.id=c.u_id and p.id=c.p_id;
創(chuàng)建視圖
create view u_p_userage as select u.id as uid,u.name as uname,p.id as pid,p.name as pname from user as u , products as p, user_product as c where u.id=c.u_id and p.id=c.p_id
對數(shù)據(jù)庫重構(gòu),卻不影響程序的運行
假設(shè)我對用戶表進行拆分,比如變成 了 id和age為一張表 id和name為一張表 那么這個時候 再去select *from user就不管用了。
那么我可以創(chuàng)建視圖 create view user as select....去重新寫sql語句這樣就能保證不改變腳本程序。
提高了安全性能。可以對不同的用戶
設(shè)定不同的視圖。例如:某用戶只能獲取user表的name和age數(shù)據(jù),不能獲取sex數(shù)據(jù)等其他數(shù)據(jù)。
create view other as select a.name, a.age from user as a;
sql語句面試題:
表內(nèi)容:
2005-05-09 勝
2005-05-09 勝
2005-05-09 負
2005-05-09 負
2005-05-10 勝
2005-05-10 負
2005-05-10 負
如果要生成下列結(jié)果, 該如何寫sql語句?
勝 負
2005-05-09 2 2
2005-05-10 1 2
select rq,sum(case when shengfu='勝' then 1 else 0 end) as '勝',sum(case when shengfu='負' then 1 else 0 end) as'負' from tmp group by rq
表中有A B C三列,用SQL語句實現(xiàn):當A列大于B列時選擇A列否則選擇B列,當B列大于C列時選擇B列否則選擇C列。
select (case when a>b then a else b end),(case when b>c then b else c end) from table_name
請取出tb_send表中日期(SendTime字段)為當天的所有記錄?(SendTime字段為datetime型,包含日期與時間)
select * from table_name where datediff(curdate(),SendTime)=0
有一張表,里面有3個字段:語文,數(shù)學,英語。其中有3條記錄分別表示語文70分,數(shù)學80分,英語58分,請用一條sql語句查詢出這三條記錄并按以下條件顯示出來(并寫出您的思路):
大于或等于80表示優(yōu)秀,大于或等于60表示及格,小于60分表示不及格。
顯示格式:
語文 數(shù)學 英語
及格 優(yōu)秀 不及格
select (case when '語文'>=80 then '優(yōu)秀' case when '語文'>=60 then ’及格‘ else '不及格' end) as '語文' , (case when '數(shù)學'>=80 then '優(yōu)秀' case when '數(shù)學'>=60 then ’及格‘ else '不及格' end) as '數(shù)學' ,(case when '英語'>=80 then '優(yōu)秀' case when '英語'>=60 then ’及格‘ else '不及格' end) as '英語' from table_name
下面附上一個習題集,大家可以去上面練習一下:
sql語句練習題及答案
MYSQL分頁查詢優(yōu)化
從性能最差的開始:
select * from table_name ordered by id limit 1000,10;
但是到百萬級數(shù)據(jù)時會變得很慢
優(yōu)化一點的語句:
SELECT * FROM table WHERE id >= (SELECT id FROM table LIMIT 1000000, 1) LIMIT 10;
因為id直接定位到1000000位置開始,而不用全表掃描過去。
下面這句可能更好一些:
SELECT * FROM table WHERE id BETWEEN 1000000 AND 1000010;
估計是因為沒有用子查詢,不會將結(jié)果存放在臨時表中再執(zhí)行第二步操作。between直接一步定位到1000000位置。
若查詢id并不連續(xù),使用IN的
SELECT * FROM table WHERE id IN(10000, 100000, 1000000...);
參考鏈接:
mysql視圖的作用(詳細)
SQL經(jīng)典面試題及答案
MySQL 百萬級分頁優(yōu)化(Mysql千萬級快速分頁)