前言:今天在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会无法使用索引的

最新文章

  1. CRL快速开发框架系列教程十二(MongoDB支持)
  2. JavaScript权威设计--CSS(简要学习笔记十六)
  3. 【转】JavaScript之web通信
  4. requst方法简单用一下
  5. PHP程序员如何突破成长瓶颈
  6. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
  7. Oracle游标
  8. 微信小程序 获取OpenId
  9. 统计学习方法 三 kNN
  10. windows转储文件(dmp)
  11. 201521123077 《Java程序设计》第7周学习总结
  12. MVC查询数据接收及校验
  13. dfs_SPFA 判负环
  14. PHP 反射的简单使用
  15. LeetCode题解之Binary Tree Level Order Traversal II
  16. 安装Linux Mint 17后要做的20件事
  17. spring boot 无法启动
  18. LintCode: Single Number
  19. ORM-Dapper快速学习
  20. web开发,让用户流水线进行操作,不可返回后退

热门文章

  1. SQL做日历
  2. [DevExpress]GridControl 同步列头checkbox与列中checkbox状态
  3. phpstorm配置取消掉63342
  4. gson小练习之嵌套复杂数据解析
  5. wordpress在Linux nginx下权限设置
  6. Nodejs & Mongod
  7. (转)《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期
  8. OFBIZ bug_ControlServlet.java:233:ERROR
  9. 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample
  10. python中变量