mysql or全表_mysql or条件可以使用索引而避免全表

在某些情况下,or条件可以避免全表扫描的。

1 .where 语句里面如果带有or条件, myisam表能用到索引, innodb不行。

1)myisam表:

CREATE TABLE IF NOT EXISTS `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `uid` (`uid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

mysql> explain select * from a where id=1 or uid =2;

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

| id | select_type | table | type        | possible_keys | key         | key_len | ref  | rows | Extra                                 |

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

|  1 | SIMPLE      | a     | index_merge | PRIMARY,uid   | PRIMARY,uid | 4,4     | NULL |    2 | Using union(PRIMARY,uid); Using where |

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

1 row in set (0.00 sec)

2)innodb表:

CREATE TABLE IF NOT EXISTS `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `uid` (`uid`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

mysql>  explain select * from a where id=1 or uid =2;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | a     | ALL  | PRIMARY,uid   | NULL | NULL    | NULL |    5 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

2 .必须所有的or条件都必须是独立索引:

+-------+----------------------------------------------------------------------------------------------------------------------

| Table | Create Table

+-------+----------------------------------------------------------------------------------------------------------------------

| a     | CREATE TABLE `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------

1 row in set (0.00 sec)

explain查看:

mysql> explain select * from a where id=1 or uid =2;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | a     | ALL  | PRIMARY       | NULL | NULL    | NULL |    5 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

全表扫描了。

3. 用UNION替换OR (适用于索引列)

通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描.

注意, 以上规则只针对多个索引列有效. 如果有column没有被索引, 查询效率可能会因为你没有选择OR而降低.

在下面的例子中, LOC_ID 和REGION上都建有索引.

高效:

selectloc_id , loc_desc , regionfromlocationwhereloc_id = 10

union

selectloc_id , loc_desc , regionfromlocationwhereregion ="melbourne"

低效:

selectloc_id , locdesc, regionfromlocationwhereloc_id = 10orregion ="melbourne"

如果你坚持要用OR, 那就需要返回记录最少的索引列写在最前面.

4. 用in来替换or

这是一条简单易记的规则,但是实际的执行效果还须检验,在oracle8i下,两者的执行路径似乎是相同的.

低效:

select…. from location where loc_id = 10 or loc_id = 20 or loc_id = 30

高效

select… from location where loc_in  in (10,20,30);

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

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

相关文章

【51Nod - 1163】最高的奖励 (贪心+优先队列 或 妙用并查集)

题干: 有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突&#xff0…

mysql varchar java_关于MySQL varchar类型最大值,原来一直都理解错了

写在前面关于MySQL varchar字段类型的最大值计算,也许我们一直都理解错误了,本文从问题出发,经实践验证得出一些实用经验,希望对大家的开发工作有些帮助~背景描述最近同事在做技术方案设计时候,考虑到一个表设计时希望…

【CodeForces - 1027C】Minimum Value Rectangle (数学,公式化简,思维,卡常卡memset)

题干: You have nn sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can …

mysql数据库业务逻辑_Mysql业务设计(逻辑设计)

逻辑设计数据库设计三大范式数据库设计第一大范式数据库表中所有的字段都只具有单一属性单一属性的列是由基本数据类型所构成设计出来的表都是简单的二维表数据库设计的第二大范式要求表中只有一个业务主键,也就是说符合第二范式的表不能存在非主键列,只…

【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)

题干: Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: First, Alice will split …

mysql一张表1亿天数据_1亿条数据在PHP中实现Mysql数据库分表100张

转:1亿条数据在PHP中实现Mysql数据库分表100张http://php-z.com/thread-2115-1-1.html(出处: PHP-Z)当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试,1亿条数据,分100张表。具体实现…

【POJ - 3273 】Monthly Expense (二分,最小最大值)

题干: Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over t…

Mysql8秒级加字段_Mysql8.0秒级加字段

Mysql 8.0版本合并了腾讯互娱数据库团队的Patch,可以实现秒级添加字段,这个功能可谓是mysql数据库攻城狮的福音,解决了之前5.6,5.7版本添加字段很高的运维成本。下面是验证mysql8.0版本秒级添加字段的过程首先用sysbench模拟一张1…

【HDU - 1009 】FatMouse' Trade (贪心)

题干: FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of ca…

lua进入压缩包_使用lua语言制作贪吃蛇游戏(love2d)(一)开发环境的搭建

本教程教大家使用lua制作一个贪吃蛇,游戏引擎使用love2d,因为它开源轻巧而且跨平台。1.开发环境搭建:windows系统:在windows系统下,首先我们进入官网www.love2d.org。love2d官网进入官网可以看到Download选项&#xff…

【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

题干: You are given an array a with n elements. Each element of a is either 0 or 1. Lets denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to…

mysql rand() 子查询_MySQL ------ 子查询(十三)

查询(query):任何SQL 都是查询,但此术语一般指select 语句子查询(subquery):嵌套在查询中的查询,MySQL4.1 引入对子查询的支持。接下来得就比较有意思了,需要你对于表与表之间的关系有所了解&am…

【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)

题干: The array a with n integers is given. Lets call the sequence of one or more consecutive elements in a segment. Also lets call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the inp…

centos 6.5 apache mysql php_CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

简单点的:1.关闭SELINUX:setenfo 0 暂时关闭2.安装Apache:yum install httpd3.安装MySQL:yum install mysql mysql-server4.安装PHP:yum install php5.相关的配置:PHP关联MySQL;httpd出错信息…

【HDU - 5585】Numbers (水题,数学,数论)

题干&#xff1a; There is a number N.You should output "YES" if N is a multiple of 2, 3 or 5,otherwise output "NO". Input There are multiple test cases, no more than 1000 cases. For each case,the line contains a integer N.(0<N<…

【HDU - 2255】奔小康赚大钱(KM算法模板,二分图最优匹配)

题干&#xff1a; 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革&#xff1a;重新分配房子。 这可是一件大事,关系到人民的住房问题啊。村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住&#xff08;如果有老百姓没房子住的话&#xff0c;容易引起…

mysql 如何调用函数结果_MySQL自定义函数调用不出结果

自定义函数的代码&#xff1a;DROP FUNCTION IF EXISTS fn_HrStaffBase_GetNameFromidCarddelimiter //CREATE FUNCTION fn_HrStaffBase_GetNameFromidCard (a VARCHAR(30))RETURNS VARCHAR(50)beginreturn (SELECT staff_name FROM hr_staff_base where idCard a);END //--…

【CodeForces - 1066A~E】水题,模拟(有技巧),思维,题意难懂的模拟,二进制问题(有技巧)

A. 题目大意&#xff1a; x坐标上1~L有L个点都是整数&#xff0c;每v个长度就有一个灯亮着&#xff0c;但是有 [ l , r ] 这段区间上有列火车挡住了&#xff0c;问你能看到多少亮灯。 解题报告&#xff1a; 大水题啊&#xff0c;找几个样例就会发现需要特殊处理一下左边界恰好…

【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)

题干&#xff1a; It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th of th…

python中with open写csv文件_Python中的CSV文件使用with语句的方式详解

是否可以直接使用with语句与CSV文件&#xff1f;能够做这样的事情似乎很自然&#xff1a;import csvwith csv.reader(open("myfile.csv")) as reader:# do things with reader但是csv.reader不提供__enter__和__exit__方法,所以这不行.但是我可以分两步做&#xff1a…