sql 简单查询修改
1.group by order by select siteid count(*)as 1 from webdb where gathtrime between '2017-06-14 00:00:00' and '2017-06-14 23:59:59' group by siteid order by 1desc 2 in limit select * from sitemanage where url in (select url from curl) limit 0,1000 3 时间 select* from sitemanage where siteid=12 and gathertime>'2017-06-12 00:00:00‘ order by gathertime desc 4 update sitemanage set updatestrategy=3 where id in (select siteid from webde where newurl!='(null)' order by siteid ASC) 5update sitemanage set =replace(url,'','') 6.SELECT id, url, configs FROM sitemanage WHERE (INSTR(configs, 'div.title>')>0) SELECT id, url, configs FROM sitemanage WHERE (INSTR(configs, '<filterreg>')=0) 7.SELECT * FROM sitemanage WHERE (INSTR(configs, 'h1')>0) ORDER BY url ASC LIMIT 2000,3283 8 update sitemanage set sitemanage.vname = (select vname from contrast where sitemanage.id=contrast.id) url中含有中文汉字 9SELECT url FROM sitemanage WHERE length(url )!=char_length(url) 10 SELECT id FROM siteid2 WHERE id NOT in (SELECT id FROM metasearchconf_id WHERE id BETWEEN 94366 AND 97000) AND (id BETWEEN 94366 AND 97000) LIMIT 31 11 update sitemanage set updatestrategy=2, configs= replace (configs,'<titlefilter><![CDATA[td.title]]></titlefilter>','<titlefilter><![CDATA[table.title>tr>td[align=center]]]></titlefilter>') WHERE url LIKE 'http://www.ln.gov.cn%' #SELECT * from sitemanage where id in (SELECT id from contrast) #update sitemanage set updatestrategy=3 where id in (SELECT id from contrast) #select count(id) from sitemanage WHERE updatestrategy=3 and (id in (SELECT id from contrast))
查询部分:
1、等值连接(from a,b)>外连接(from a left join b)>子查询。
2、依照小结果驱集驱动大结果集的原则写where条件的顺序。
3、不使用not in ,可用 not exists代替。
4、尽量减少对字符串、时间类型字段用>、<,可用between and代替。 条件列中不可使用函数 例如:Year(publishtime)>2016。
5、多表查询中数据量大时,用关联代替 in,不要使用or 连接。
6、养成用explain 检测语句效率
7、多表查询最好的优化方式是建视图,提高表的逻辑独立性,还可以屏蔽原有表结构变化带来的影响。
索引部分:
1、一个查询只能用到一个索引,也就是where条件与索引配合使用的必要性。
2、选择建立单列索引还是复合索引,复合索引同样是依照从左到右的顺序,而且一定要将使用频率最高的字段放在左边。若第一个没有与条件匹配到,则其他字段的索引不会被使用。
3、关联查询时,条件两边字段类型相同并且都建有索引才会提速。
4、建过索引的字段默认值不要为null
5、Like ‘%XXX%’是不会用到索引的,但like ‘xxxx%’会被用到。
6、尽量不要对大文本或长字符串建索引,可以建前缀索引。
7、经常有删除操作的表、或有大量长文本的表要定期进行碎片整理。用MyISAM引擎为例,我们对表做删除操作时,data中的*.MYD文件的大小是不会变小,会影响我们的存储与查询。所以要定期做清理(OPTIMIZE TABLE `***`可多个表‘,’连接)
8\\表中时间段查询语句
SELECT COUNT(url) FROM wise_web_docinfo WHERE gathertime BETWEEN '2016-05-30 00:00:00' AND '2016-05-30 23:00:00'
9\\url 模糊查询
SELECT * FROM sitemanage WHERE url LIKE '%0634%'
10\\根据url字段,更改表中的其他字段
UPDATE sitemanage SET category='论坛D' ,updatestrategy=2 WHERE url LIKE '%0634%'
11\\in条件,limit条件查询
SELECT * FROM webdb WHERE url in(select url from curl ) LIMIT 0,1000
12\\时间查询
SELECT?*?FROM?wise_web_docinfo?WHERE??site_id?=-7651?AND?gathertime?>'2016-07-12?00:00:00'?ORDER?BY?gathertime?DESC
13\\in
SELECT * FROM sitemanage WHERE id IN (SELECT siteid FROM waitlist WHERE newurl != '(NULL)' ORDER BY siteid ASC)
14
UPDATE sitemanage SET updatestrategy=3 WHERE id IN (SELECT siteid FROM waitlist WHERE newurl != '(NULL)' ORDER BY siteid ASC)
15
SELECT * FROM `thai_com` ORDER BY gathertime LIMIT 0, 10;
16
update `baidutieba` set url= replace (url,'http://tieba.baidu.com//f?kw','http://tieba.baidu.com/f?kw')