常见sql技巧

一、一些常见的SQL实践

1)负向条件查询不能使用索引

  • select * from order where status!=0 and stauts!=1

  not in/not exists都不是好习惯

可以优化为in查询:

  • select * from order where status in(2,3)

2)前导模糊查询不能使用索引

  • select * from order where desc like '%XX'

而非前导模糊查询则可以:

  • select * from order where desc like 'XX%'

3)数据区分度不大的字段不宜使用索引

  • select * from user where sex=1

原因:性别只有男,女,每次过滤掉的数据很少,不宜使用索引。

经验上,能过滤80%数据时就可以使用索引。对于订单状态,如果状态值很少,不宜使用索引,如果状态值很多,能够过滤大量数据,则应该建立索引。

4)在属性上进行计算不能命中索引

  • select * from order where YEAR(date) < = '2017'

即使date上建立了索引,也会全表扫描,可优化为值计算:

  • select * from order where date < = CURDATE()

或者:

  • select * from order where date < = '2017-01-01'

二、并非周知的SQL实践

5)如果业务大部分是单条查询,使用Hash索引性能更好,例如用户中心

  • select * from user where uid=?

  • select * from user where login_name=?

原因:

B-Tree索引的时间复杂度是O(log(n))

Hash索引的时间复杂度是O(1)

 

6)允许为null的列,查询有潜在大坑

单列索引不存null值,复合索引不存全为null的值,如果列允许为null,可能会得到“不符合预期”的结果集

  • select * from user where name != 'shenjian'

如果name允许为null,索引不存储null值,结果集中不会包含这些记录。

所以,请使用not null约束以及默认值。

7)复合索引最左前缀,并不是值SQL语句的where顺序要和复合索引一致

用户中心建立了(login_name, passwd)的复合索引

  • select * from user where login_name=? and passwd=?

  • select * from user where passwd=? and login_name=?

都能够命中索引

  • select * from user where login_name=?

也能命中索引,满足复合索引最左前缀

  • select * from user where passwd=?

不能命中索引,不满足复合索引最左前缀

8)使用ENUM而不是字符串

ENUM保存的是TINYINT,别在枚举中搞一些“中国”“北京”“技术部”这样的字符串,字符串空间又大,效率又低。

三、小众但有用的SQL实践

9)如果明确知道只有一条结果返回,limit 1能够提高效率

  • select * from user where login_name=?

可以优化为:

  • select * from user where login_name=? limit 1

原因:

你知道只有一条结果,但数据库并不知道,明确告诉它,让它主动停止游标移动

10)把计算放到业务层而不是数据库层,除了节省数据的CPU,还有意想不到的查询缓存优化效果

  • select * from order where date < = CURDATE()

这不是一个好的SQL实践,应该优化为:

$curDate = date('Y-m-d');

$res = mysql_query(

    'select * from order where date < = $curDate');

原因:

释放了数据库的CPU

多次调用,传入的SQL相同,才可以利用查询缓存

11)强制类型转换会全表扫描

  • select * from user where phone=13800001234

你以为会命中phone索引么?大错特错了,这个语句究竟要怎么改?

末了,再加一条,不要使用select *(潜台词,文章的SQL都不合格 =_=),只返回需要的列,能够大大的节省数据传输量,与数据库的内存使用量哟。

 

原文链接:https://mp.weixin.qq.com/s/ap9tkaEuWDi39u0NFxnACA

转载于:https://www.cnblogs.com/Terry-Wu/p/7605845.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428710.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1466C. Canine poetry

C. Canine poetry&#xff1a;题目 题意&#xff1a;可以用任何小写英文字母替换其中的&#xff0c;使其没有回文子串 思路&#xff1a;如果两个相同就换&#xff0c;如果三个是回文也换&#xff0c;先看两个再看三个#include <bits/stdc.h> using namespace std; typed…

BZOJ 2768 [JLOI2010]冠军调查

还说还剩十分钟A一道水题&#xff0c;然后发现和善意的投票一模一样粘个代码过去直接A。。。 装作自己又写了一道题。 题面 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<…

1420C1. Pokémon Army (easy version)

C1. Pokmon Army (easy version)&#xff1a;题目 题意&#xff1a;选择其中一部分&#xff0c;按照-依此计算&#xff0c;求总和 思路&#xff1a;找到局部最大值&#xff0c;然后减去局部最小值&#xff0c;依此找。#include <bits/stdc.h> using namespace std; type…

python学习笔记(五)缩进

python学习笔记&#xff08;五&#xff09;缩进 原作&#xff1a;http://www.cnblogs.com/vamei/archive/2012/05/29/2524706.html 笔记&#xff1a; #!/usr/bin/env python i 1 #把1赋值给i变量 x 2 #把2赋值给x变量 if i > 0: #如果i大于1x x 1#执行x1 print (x)#输出…

1443B. Saving the City

B. Saving the City&#xff1a;题目 题意&#xff1a;1是炸弹&#xff0c;引爆的同时引爆i-1&#xff0c;i1&#xff0c;埋一个炸弹的成本为b,引爆的成本为a 思路&#xff1a;首先如果有炸弹&#xff0c;必须引爆一次&#xff0c;然后往后遍历&#xff0c;看是引爆还是接上的…

【OpenCV函数】轮廓提取;轮廓绘制;轮廓面积;外接矩形

FindContours 在二值图像中寻找轮廓 int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour, int header_sizesizeof(CvContour), int modeCV_RETR_LIST, int methodCV_CHAIN_APPROX_SIMPLE, CvPoint offsetcvPoint(0,0) ); image 输入的 8-比…

【AtCoder Regular Contest 082】Derangement

【链接】点击打开链接 【题意】 在这里写题意【题解】 贪心。连续一块的p[i]i的话,对答案的贡献就应该为(这个连续块的长度1)/2;长度为1的也正确.(也即两两相邻的互换位置。)【错的次数】 0【反思】 在这了写反思【代码】 #include <cstdio> #include <iostream> #…

1618D. Array and Operations

D. Array and Operations&#xff1a;题目 之前写的了&#xff0c;直接上答案&#xff01; #include <bits/stdc.h> #define int long long #define pII pair<int, int> #define endl "\n" #define fast ios_base::sync_with_stdio(0), cin.tie(0), cou…

kendo 服务端排序

z折腾了一天。。 参考 &#xff1a;https://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.sortable.compare 关键代码 parameterMap: function(data, type) {if(data.sort) {var sortObj data.sort[0];typeSort sortObj.dir;typeName sortObj.f…

416B. Art Union

B. Art Union&#xff1a;题目 思路&#xff1a;dp那味道来了#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); int g[50051][11]; int main() {int m,n;//画的数量&#xff0c;和画家…

Windows下编译TensorFlow1.3 C++ library及创建一个简单的TensorFlow C++程序

由于最近比较忙&#xff0c;一直到假期才有空&#xff0c;因此将自己学到的知识进行分享。如果有不对的地方&#xff0c;请指出&#xff0c;谢谢&#xff01;目前深度学习越来越火&#xff0c;学习、使用tensorflow的相关工作者也越来越多。最近在研究tensorflow线下采用 pytho…

665C. Simple Strings

C. Simple Strings&#xff1a;题目 思路&#xff1a;简单的离谱&#xff0c;就相同就换就行&#xff0c;哪有dp的事啊#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; i…

798B. Mike and strings

B. Mike and strings&#xff1a;题目 思路&#xff1a;纯暴力题&#xff0c;不想写&#xff0c;copy了别人代码#include<iostream> #include<string> using namespace std; void change(string &s){ss.substr(1)s.substr(0,1); } int main(){int n;cin>&g…

jquery ajax 数据传输

在 form表单中&#xff0c;需要发送给后台的是一串长数据&#xff0c;后台才能接受&#xff0c;而用户则只需要输入字符串中的一部分&#xff0c;这种情况下&#xff0c;就需要将用户输入内容&#xff0c;和剩余部分进行拼串&#xff0c;然后添加进 formData 中传输。另一种情况…

1315B. Homecoming

B. Homecoming&#xff1a;题目 思路&#xff1a;从后往前遍历就行了#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; int main() {int t;cin >> t;while (t--){in…

jquery datatable 获取当前分页的数据

使用jquery datatable 遇到分页分别求和时&#xff0c;找了半天才找到获取当前分页数据的方法&#xff0c;以此总结 var table$(#example).DataTable( {"pagingType": "full_numbers",} ); table.columns({page:current}).data();//当前页面里的数据 转载于…

870C. Maximum splitting

C. Maximum splitting&#xff1a;题目 这凭啥1300分&#xff1f;#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; int main() {int t;cin>>t;while (t--){int n;c…

useGeneratedKeys的用法

<!-- useGeneratedKeys"true"把新增加的主键赋值到自己定义的keyProperty&#xff08;id&#xff09;中 --> <insert id"insert" useGeneratedKeys"true" keyProperty"id" parameterType""> 转载于:https://www…

1040B. Shashlik Cooking

B. Shashlik Cooking&#xff1a;题目 思路&#xff0c;如果所有烤肉都可以一次就行&#xff0c;如果不行就去前面调整一下#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); int main(…

粉红小猪中有一个叫“快乐小鸡”的游戏

最近在学习svg,书看了一本 然后再找了个框架 snap.svg 摸索着写了个游戏给女儿玩。哈哈。即涨知识又娱乐。 转载于:https://www.cnblogs.com/goldli/p/7649898.html