易截截图软件、单文件、免安装、纯绿色、仅160KB

mysql sql 百万级数据库优化方案

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎
放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num上设置
默认值0,确保表中num列没有null值,然后这
样查询:
select id from t where num=0
3.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。
4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:
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
5.in 和 not in 也要慎用,否则会导致全表扫描,如:
select id from t where num in(1,2,3)
对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3
6.下面的查询也将导致全表扫描:
select id from t where name like ‘%abc%’
若要提高效率,可以考虑全文检索。
7.如果在 where 子句中使用参数,也会导致全表扫描。因为SQL
只有在运行时才会解析
局部变量,但优化程序
不能将访问
计划的选择推迟到运行时;它必须在编译
时进行选择。然
而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where [email=num=@num]num=@num[/email]
<[email=num=@num]mailto:num=@num[/email]>
可以改为强制查询使用索引:
select id from t with(index(索引名)) where [email=num=@num]num=@num[/email]
<[email=num=@num]mailto:num=@num[/email]>
8.应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:
select id from t where num/2=100
应改为:
select id from t where num=100*2
9.应尽量避免在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 &lsquo


相关文档:

Mysql数据库表名空格处理

今天使用PowerDesigner搭建了一个小项目,最后执行完SQL脚本时才发现有2张表的表名中间多了一个空格,查完资料,最后并惊奇发现映射文件竟然多了2个`,就是shift+数字键1的那个,也就是他在脚本是这样写的:
create table `table1`(....);
问题解决。
PS:从网上找到的资料:
作为一个Oracle DBA,MySQL很多SQL语法与Orac ......

一个复杂的mysql查询语句(case,when,then,left join )

select `a`.`id` AS `id`,`a`.`UserName` AS `UserName`,
(case when (`a`.`sRegDate` = _utf8'1990-01-01 00:00:00.0') then _utf8'' else cast(date_format(`a`.`sRegDate`,_utf8'%Y-%m-%d %H:%i:%S') as char charset utf8) end) AS `sRegDate`,(case when (`a`.`feeendtime` = _utf8'1990-01-01 00:00:00.0') then _ut ......

一个项目涉及到的50个SQL语句

 /* 
 标题:一个项目涉及到的50个SQL语句(整理版) 
 作者:爱新觉罗.毓华
 时间:2010-05-10 
 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 
 问题及描述: 
 --1.学生表 
 Student(S#,Sname,Sage,Ssex) ......

sql 中case用法

Case具有两种格式。简单Case函数和Case搜索函数。
--简单Case函数
CASE sex
         WHEN '1' THEN '男'
         WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函数
CASE WHEN sex = '1' THEN '男'
     ......

SQL触发器实例

定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序。触发器是一个特殊的存储过程。
      常见的触发器有三种:分别应用于Insert , Update , Delete 事件。
      我为什么要使用触发器?比如,这么两个表:
& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号