首页 > 技术文章 > 数据库常见面试题

xiuxiu55 2017-04-28 09:06 原文

数据库事务:

是一系列的数据库操作,是数据库应用的基本逻辑单位。事务性质:原子性、
1)原子性。即不可分割性,事务要么全部被执行,要么就全部不被执行。
2)一致性或可串性。事务的执行使得数据库从一种正确状态转换成另一种正确状态
3)隔离性。在事务正确提交之前,不允许把该事务对数据的任何改变提供给任何其他事务,
4)持久性。事务正确提交后,其结果将永久保存在数据库中,即使在事务提交后有了其他故障,事务的处理结果也会得到保存。

-----------------------------------------------------------------------------------------------------------------------------------

数据库的内联,外联:

外联:left join/right join

-------------------------------------------------
table1        |  table2     
-------------------------------------------------
id  name    |  id  score   
1  lee     |  1  90    
2  zhang    |  2  100    
4  wang     |  3  70    
-------------------------------------------------

eg.

left join: select *from table1 left join table2 on table1.id=table2.id

-------------结果-------------
id  name    id    score
------------------------------
1  lee       1    90
2  zhang    2    100
4  wang     NULL   NULL
------------------------------

right join:select *from table1 right join table2 on table1.id = table2.id

-------------结果-------------
id    name    id  score
------------------------------
1    lee      1    90
2    zhang     2    100
NULL  NULL     3    70
------------------------------

full join:select *from table1 full join table2 on table2.id = table2.id

-------------结果-------------
id    name     id    score
------------------------------
1    lee      1    90
2    zhang     2    100
4    wang     NULL  NULL
NULL  NULL     3    70
------------------------------

inner join:

select *from table1 join table2 on table1.id=table2.id


-------------结果-------------
id  name    id  score
------------------------------
1  lee       1  90
2  zhang    2  100
------------------------------

-----------------------------------------------------------------------------------------------------------------------------------

数据库常见性能优化方案:

 

~对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

~应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:

 

~应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。

~应尽量避免在 where 子句中使用 or 来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描

~in 和 not in 也要慎用,否则会导致全表扫描

 

~下面的查询也将导致全表扫描:

select id from t where name like ‘%abc%’

若要提高效率,可以考虑全文检索。

~应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:

select id from t where substring(name,1,3) = ’abc’       -–name以abc开头的id
select id from t where datediff(day,createdate,’2005-11-30′) = 0    -–‘2005-11-30’    --生成的id

 

应改为:

 

select id from t where name like 'abc%'
select id from t where createdate >= '2005-11-30' and createdate < '2005-12-1'

 

~不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。

~在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,否则该索引将不会被使用,并且应尽可能的让字段顺序与索引顺序相一致。


~对于多张大数据量(这里几百条就算大了)的表JOIN,要先分页再JOIN,否则逻辑读会很高,性能很差。

~任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。

~在新建临时表时,如果一次性插入数据量很大,那么可以使用 select into 代替 create table,避免造成大量 log ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先create table,然后insert。

 

~尽量避免大事务操作,提高系统并发能力。

 

推荐阅读