前言:今天在PostgreSQL中使用like,字段类型是int,执行语句报错,

1.表结构:都是用sysbench工具产生的

postgres=# \d sbtest1;
                             Table "public.sbtest1"
 Column |      Type      |                      Modifiers                       
--------+----------------+------------------------------------------------------
 id     | integer        | not null default nextval('sbtest1_id_seq'::regclass)
 k      | integer        | not null default 0
 c      | character(120) | not null default ''::bpchar
 pad    | character(60)  | not null default ''::bpchar
Indexes:
    "sbtest1_pkey" PRIMARY KEY, btree (id)
    "k_1" btree (k)
MySQL

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| k     | int(10) unsigned | NO   | MUL | 0       |                |
| c     | char(120)        | NO   |     |         |                |
| pad   | char(60)         | NO   |     |         |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)
2.在PostgreSQL里面执行语句

postgres=# select k from sbtest1 where k like '3%' limit 10;
ERROR:  operator does not exist: integer ~~ unknown
LINE 1: select k from sbtest1 where k like '3%' limit 10;
                                      ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

那么我们转换一下类型试一下,果然是可以了,那么意味着有索引页无法使用了

postgres=# select k from sbtest1 where k::text like '3%' limit 10;
   k   
-------
 39162
 39075
 37436
 36750
 34065
 37606
 38584
 34175
 35533
 38342
(10 rows)

果断全表扫描
postgres=# explain select k from sbtest1 where k::text like '3%' limit 10;
                            QUERY PLAN                             
-------------------------------------------------------------------
 Limit  (cost=0.00..63.64 rows=10 width=4)
   ->  Seq Scan on sbtest1  (cost=0.00..9577.31 rows=1505 width=4)
         Filter: ((k)::text ~~ '3%'::text)
(3 rows)

3.那么来看看MySQL是怎么样子滴,
不会报错,可以执行,是不是MySQL自动转换类型了,来个执行计划就懂了
(twDB)root@localhost [sysbench]> select k from sbtest1 where k like '3%' limit 10;
+------+
| k    |
+------+
|  319 |
| 3002 |
| 3073 |
| 3130 |
| 3173 |
| 3181 |
| 3211 |
| 3274 |
| 3279 |
| 3385 |
+------+
10 rows in set (0.00 sec)

哈哈,果然使用索引
(twDB)root@localhost [sysbench]> explain select k from sbtest1 where k like '3%' limit 10;
+----+-------------+---------+-------+---------------+------+---------+------+--------+--------------------------+
| id | select_type | table   | type  | possible_keys | key  | key_len | ref  | rows   | Extra                    |
+----+-------------+---------+-------+---------------+------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | sbtest1 | index | k_1           | k_1  | 4       | NULL | 236197 | Using where; Using index |
+----+-------------+---------+-------+---------------+------+---------+------+--------+--------------------------+
1 row in set (0.03 sec)

(twDB)root@localhost [sysbench]>

结论
在PostgreSQL中,千万别对int类型进行like会无法使用索引的

PostgreSQL和MySQL like区别的更多相关文章

  1. PostgreSQL与MySQL的区别收集(转)

    特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...

  2. 【转】PostgreSQL与MySQL比较

    转自:https://www.cnblogs.com/geekmao/p/8541817.html PostgreSQL与MySQL比较   特性 MySQL PostgreSQL 实例 通过执行 M ...

  3. [转帖]PostgreSQL与MySQL比较 From 2010年

    PostgreSQL与MySQL比较 [复制链接]  http://bbs.chinaunix.net/thread-1688208-1-1.html osdba 稍有积蓄 好友 博客 消息 论坛徽章 ...

  4. PostgreSQL 优势,MySQL 数据库自身的特性并不十分丰富,触发器和存储过程的支持较弱,Greenplum、AWS 的 Redshift 等都是基于 PostgreSQL 开发的

    PostgreSQL 优势 2016-10-20 21:36 686人阅读 评论(0) 收藏 举报  分类: MYSQL数据库(5)  PostgreSQL 是一个自由的对象-关系数据库服务器(数据库 ...

  5. PostgreSQL 和 MySQL 在用途、好处、特性和特点上的异同

    PostgreSQL 和 MySQL 在用途.好处.特性和特点上的异同. PostgreSQL 和 MySQL 是将数据组织成表的关系数据库.这些表可以根据每个表共有的数据链接或关联.关系数据库使您的 ...

  6. 利用Navicat高效率postgresql转mysql数据库

    本人很喜欢postgresql数据库,也一直认为postgresql比mysql要更好更强大. 可生态环境太差了,无奈,最近要把一个小站转成mysql数据库. 小站主要表数据110万,pg_dump备 ...

  7. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)

    [评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...

  8. Redis的概念及与MySQL的区别

    学了MySQL相关知识后,了解到很多公司都会用mysql+redis互补使用的,今天学习整理一下Redis的相关知识. 首先是Redis和MySQL的区别: MySQL是典型的关系型数据库:Redis ...

  9. PostgreSQL与MySQL常用命令比较[转]

    PostgreSQL与MySQL常用命令比较 原文链接: http://www.phpwell.com/?p=174 PostgreSQL MySQL 服务启动:1)#service postgres ...

随机推荐

  1. Memcached的安装(Linux)、操作、命令

    最近在整理有关分布式缓存的服务器,做了一下老牌nosql服务器memcached的学习总结.文中所述的所有安装均是在联网的情况下进行的. 序: 什么是memcached: Free & ope ...

  2. net发布mvc项目

    1.复制 Web 文件夹 2.复制 DLL 文件C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies C ...

  3. php下删除一篇文章生成的多个静态页面

    php自定义函数之删除一篇文章生成的多个静态页面,可能有多页的文章,都是需要考虑到的. 复制代码代码如下: //– 删除一篇文章生成的多个静态页面  //– 生成的文章名为 5.html 5_2.ht ...

  4. Spark Streaming揭秘 Day12 数据安全容错(Executor篇)

    Spark Streaming揭秘 Day12 数据安全容错(Executor篇) 今天,让我们研究下SparkStreaming在Executor端的数据安全及容错机制. 在SparkStreami ...

  5. Hello World 的makefile模板及其分析

    makefile模板: ifeq ($(KERNELRELEASE),) //判断KERNELRELEASE是否为空,只有执行make的当前目录为内核源代码目录时,该变量才不为空. KERNELDIR ...

  6. Python学习_算数运算函数

    记录以grades列表为例,分别定义输出.求和.平均值.方差和标准差函数,并输出相应的值 grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90 ...

  7. parsing html in asp.net

    http://stackoverflow.com/questions/5307374/parsing-html-page-in-asp-net http://htmlagilitypack.codep ...

  8. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

  9. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  10. JAVA多线程synchronized详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 当两个并发线程访问同一个对象object中的这个synchronized(this)同 ...