exists

SQL查询中in、exists、not in、not exists的用法与区别

与世无争的帅哥 提交于 2020-01-09 17:00:46
1、in和exists in是把外表和内表作hash(字典集合)连接,而exists是对外表作循环,每次循环再对内表进行查询。一直以来认为exists比in效率高的说法是不准确的,如果查询的两个表大小相当,那么用in和exists差别不大;如果两个表中一个较小一个较大,则子查询表大的用exists,子查询表小的用in。 例如:表A(小表),表B(大表) 方式一:索引使用 1)select * from A where id in(select id from B)   -->效率低,用到了A表上id列的索引 2)select * from A where exists(select id from B where id=A.id) -->效率高,用到了B表上id列的索引 3)select * from B where id in(select id from A)   -->效率高,用到了B表上id列的索引 4)select * from B where exists(select id from A where id=B.id) -->效率低,用到了A表上id列的索引 方式二:遍历使用 1)in()只执行一次,它查出B表中的所有id字段并缓存起来。然后检查A表的id是否与B表中的id相等,如果相等则将A表的记录加入结果集中,直到遍历完A表的所有记录。

oracle中的exists 和not exists 用法

邮差的信 提交于 2020-01-09 04:41:43
exists (sql 返回结果集为真) not exists (sql 不返回结果集为真) 如下: 表A ID NAME 1 A1 2 A2 3 A3 表B ID AID NAME 1 1 B1 2 2 B2 3 2 B3 表A和表B是1对多的关系 A.ID => B.AID SELECT ID,NAME FROM A WHERE EXIST (SELECT * FROM B WHERE A.ID=B.AID) 执行结果为 1 A1 2 A2 原因可以按照如下分析 SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=1) --->SELECT * FROM B WHERE B.AID=1有值返回真所以有数据 SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=2) --->SELECT * FROM B WHERE B.AID=2有值返回真所以有数据 SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=3) --->SELECT * FROM B WHERE B.AID=3无值返回真所以没有数据 NOT EXISTS 就是反过来 SELECT ID

19条MySQL优化准则

懵懂的女人 提交于 2020-01-08 04:36:33
1、EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划。 下面来个简单的示例,标注(1、2、3、4、5)我们要重点关注的数据: type列 , 连接类型。一个好的SQL语句至少要达到range级别。杜绝出现all级别。 key列, 使用到的索引名。如果没有选择索引,值是NULL。可以采取强制索引方式。 key_len列, 索引长度。 rows列, 扫描行数。该值是个预估值。 extra列, 详细说明。注意,常见的不太友好的值,如下:Using filesort,Using temporary。 2、SQL语句中IN包含的值不应过多 MySQL对于IN做了相应的优化,即将IN中的常量全部存储在一个数组里面,而且这个数组是排好序的。但是如果数值较多,产生的消耗也是比较大的。再例如:select id from t where num in(1,2,3) 对于连续的数值,能用between就不要用in了;再或者使用连接来替换。 3、SELECT语句务必指明字段名称 SELECT*增加很多不必要的消耗(CPU、IO、内存、网络带宽);增加了使用覆盖索引的可能性;当表结构发生改变时,前断也需要更新。所以要求直接在select后面接上字段名。 4、当只需要一条数据的时候,使用limit 1 这是为了使EXPLAIN中type列达到const类型 5

干货!SQL性能优化,书写高质量SQL语句

回眸只為那壹抹淺笑 提交于 2020-01-07 20:40:48
写SQL语句的时候我们往往关注的是SQL的执行结果,但是是否真的关注了SQL的执行效率,是否注意了SQL的写法规范? 以下的干货分享是在实际开发过程中总结的,希望对大家有所帮助! 1. limit分页优化 当偏移量特别大时,limit效率会非常低。 SELECT id FROM A LIMIT 1000,10 很快 SELECT id FROM A LIMIT 90000,10 很慢 方案一: select id from A order by id limit 90000,10; 复制代码 如果我们结合order by使用。很快,0.04秒就OK。 因为使用了id主键做索引! 当然,是否能够使用索引还需要根据业务逻辑来定,这里只是为了提醒大家,在分页的时候还需谨慎使用! 方案二 select id from A order by id between 90000 and 90010; 复制代码 2.利用limit 1 、top 1 取得一行 有些业务逻辑进行查询操作时(特别是在根据某一字段DESC,取最大一笔).可以使用limit 1 或者 top 1 来终止[数据库索引]继续扫描整个表或索引。 反例 SELECT id FROM A LIKE 'abc%' 复制代码 正例 SELECT id FROM A LIKE 'abc%' limit 1 复制代码 3. 任何情况都不要用

Conditional query in SQLite

﹥>﹥吖頭↗ 提交于 2020-01-07 07:48:29
问题 Way to do in sqlite what is typical in most DB? if exists(select 1 from tosync where tbname = "%s" and tbid = %d and (act = 1 and %d = 3 or act = 3 and %d = 1) begin delete from tosync where tbname = "%s" and tbid = %d end else begin insert into tosync(tbname, tbid, act) values("%s", %d, %d); end Replaced values respectively are [TbName, tbid, act, act, TbName, tbid, TbName, tbid, act] Please note, that this topic is not about UPSERT and similar issues available in sqlite. 回答1: You can't do

原创 | 一篇解决Springboot 整合 Elasticsearch

六月ゝ 毕业季﹏ 提交于 2020-01-06 17:15:24
大家好,我是润森。 昨天我发现 IDEA 过期了,十分钟解决了。公众号回复 IDEA 就ok ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务。 ElasticSearch 是一个基于 Lucene 的搜索服务器,其实就是对 Lucene 进行封装,提供了 REST API 的操作接口。 ElasticSearch 作为一个高度可拓展的开源全文搜索和分析引擎,可用于快速地对大数据进行存储,搜索和分析。 ElasticSearch 主要特点:分布式、高可用、异步写入、多API、面向文档 。 ElasticSearch 核心概念:近实时,集群,节点(保存数据),索引,分片(将索引分片),副本(分片可设置多个副本) 。它可以快速地储存、搜索和分析海量数据。 Docker搭建 Elasticsearch 安装Elasticsearch的Docker镜像 目前Elasticsearch 版本到了7.X, Springboot 目前不支持7.X以上的elasticsearch。所以还是选择了稳定的2.4.6 ubuntu @VM - 0 - 5 - ubuntu : ~ $ docker pull elasticsearch : 2.4 .6 2.4 .6 : Pulling from

Mysql任务调度

£可爱£侵袭症+ 提交于 2020-01-06 14:17:13
Mysql任务调度 Event调度配置 Mysql任务调度Event不执行 Mysql任务作业Event不执行 我采用的方法就是: 方法一:找到当前使用的 .cnf 文件,在 [mysqld] 的下面加入如下行 event_scheduler=1 。 MySQL5.1.x 版本中引入了一项新特性 EVENT ,顾名思义就是事件、定时任务机制,在指定的时间单元内执行特定的任务,因此今后一些对数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能。此功能是对 Oracle 的 Job/Schedule 的模仿, 5.1.X 版本以上才支持。 创建 EVENT 语法如下: CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT ''comment''] DO sql_statement; schedule: AT timestamp [+ INTERVAL interval] | EVERY interval [STARTS timestamp [+ INTERVAL interval]]

Select from 2 tables. Query = table1 OR table1 + table2

痞子三分冷 提交于 2020-01-06 04:04:23
问题 I have 2 tables with a 1:1 relation. I'm trying to select everything from table1 and table2 using PK from table1. I want to select everything from table1 and, IF table1 PK = table2 FK THEN (and, only THEN) select from table2. To make it short, I want to select from table1 even if the query result doesn't have any relation in table2. If it has, then select from table2 as well. Anyone know how to do this? 回答1: It's a left outer join select table1.*, table2.* from table1 left outer join table2

how can I use exists column with laravel model

本秂侑毒 提交于 2020-01-05 07:50:13
问题 I have a column in my database called exists , but when I try to use $model->exists Laravel checks if the record exists rater return the value of exists. Is their any way to tell Laravel not to do this on that particular model? 回答1: As suggested earlier, renaming your column would be a good idea, because it's a reserved keyword in most database servers. To answer your question though, you can use $model->getAttribute('exists') to get the value of a model attribute. Source 回答2: There's another

Using IF EXISTS (SELECT …) in a BEFORE INSERT trigger (Oracle)

你说的曾经没有我的故事 提交于 2020-01-03 18:31:06
问题 The code I have doesn't work, Oracle tells me the trigger has been created with build errors. Apparently I can't get any more precise information about what the build error is... I really haven't done a lot of SQL before so I'm not that familiar with the syntax. I have a hunch it's my IF EXISTS (SELECT ...) THEN statement that Oracle doesn't like, I've been Googling for similar examples but I couldn't really find anything that worked in my situation. So about the code: "debut" is a date