1.mysql事务隔离级别mysql
可参考sql
1.http://blog.csdn.net/taylor_tao/article/details/7063639mysql优化
2.http://xm-king.iteye.com/blog/770721性能
2.mysql优化大数据
1.对查询进行优化,应尽可能避免全表扫描优化
首先应考虑在 where 及 order by 涉及的列上创建索引.net
2.会出现全表扫描的状况设计
select * from xx where a!='aaaa'
select * from xx where a<>'aaaa'
where num is null 解决方案 能够在num上设置默认值0,确保表中num列没有null值,而后这样查询: select id from t where num=0
select id from t where num=10 or num=20 解决方案 能够这样查询: select id from t where num=10 union all select id from t where num=20
select id from t where name like '%abc%'
select id from t where num in(1,2,3) 解决方案 对于连续的数值,能用 between 就不要用 in 了: select id from t where num between 1 and 3
若是在 where 子句中使用参数,也会致使全表扫描。由于SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,若是在编译时创建访问计划,变量的值仍是未知的,于是没法做为索引选择的输入项。以下面语句将进行全表扫描: select id from t where num=@num 解决方案 能够改成强制查询使用索引: select id from t with(index(索引名)) where num=@num
应尽可能避免在 where 子句中对字段进行表达式操做,这将致使引擎放弃使用索引而进行全表扫描。如: select id from t where num/2=100 解决方案 应改成: select id from t where num=100*2
3.其余优化code
不少时候用 exists 代替 in 是一个好的选择: select num from a where num in(select num from b) 用下面的语句替换: select num from a where exists(select 1 from b where num=a.num)
并非全部索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段sex,male、female几乎各一半,那么即便在sex上建了索引也对查询效率起不了做用。
索引并非越多越好,索引当然能够提升相应的 select 的效率,但同时也下降了 insert 及 update 的效率,由于 insert 或 update 时有可能会重建索引,因此怎样建索引须要慎重考虑,视具体状况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有必要。
尽可能使用数字型字段,若只含数值信息的字段尽可能不要设计为字符型,这会下降查询和链接的性能,并会增长存储开销。这是由于引擎在处理查询和链接时会逐个比较字符串中每个字符,而对于数字型而言只须要比较一次就够了
尽量的使用 varchar/nvarchar 代替 char/nchar ,由于首先变长字段存储空间小,能够节省存储空间,其次对于查询来讲,在一个相对较小的字段内搜索效率显然要高些
任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段
避免频繁建立和删除临时表,以减小系统表资源的消耗
尽可能避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理。
本文整理自 -> 更多请见 http://www.javashuo.com/article/p-djmqyjdr-k.htmlblog