SQL语句查询和查询速度的提升

来源:互联网 发布:itunes for windows 编辑:程序博客网 时间:2024/06/05 01:05

常用的SQL查询语句:

基本查询语句

'①<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">存在两条完全相同的纪录</span>
<span style="font-family: Arial;"><span style="font-size: 14px;">'</span><span style="font-size:14px;">distinct去掉重复的行</span></span>
<span style="font-family: Arial; font-size: 14px;">select distinct * from table(表名) where (条件)'</span>

②存在部分字段相同的纪录(有主键id即唯一键)'如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
'③没有唯一键ID
'这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:select identity(int1,1) as id,* into newtable(临时表) from tableselect * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])drop table newtable

修改列名:

select column_name  as  new_name,......from  table_name 

模糊查询

’查询以李开头的用户名信息‘‘%李%’含有a的用户名select  *from users where username like '李%';’like运算副只用于字符串,所以仅与char和varchar数据类型联合使用

提高SQL查询的速度的方法:

  1、不要使用游标   

  2、规范化你的数据表   
  3、不要使用SELECT.   *   
  4、了解你将要对数据进行的操作   
  5、不要给“性别”列创建索引   
  6、使用事务   
  7、小心死锁     8、不要打开大的数据集   
  9、不要使用服务器端游标   
  10、使用参数查询   
  11、在程序编码时使用大数据量的数据库   
  12、不要使用insert导入大批的数据   
  13、注意超时问题   
  14、不要忽略同时修改同一记录的问题   
  15、在细节表中插入纪录时,不要在主表执行select max(id)
  16、避免将列设为NULLable   
  17、尽量不要使用TEXT数据类型   
  18、尽量不要使用临时表   
  19、学会分析查询   
  20、使用参照完整性
0 0