微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

比较PostgreSQL和MySQL上的查询

祝你今天愉快!

在使用Debian 7.1的小型VM上的Windows Azure上,我安装了MySQL 5.5.31和Postgresql 9.2.4.
插入和选择查询将通过pdo从PHP进行.

创建表:

MysqL的:

CREATE TABLE `test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `fdate` datetime NOT NULL,
  `ftext` varchar(1000) COLLATE utf8_unicode_ci DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `ix_date` (`fdate`),
  KEY `ix_text` (`ftext`(255))
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

PGsql

CREATE TABLE test
(
  fdate timestamp without time zone,
  ftext character varying(1000),
  id bigserial NOT NULL,
  CONSTRAINT test_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE test
  OWNER TO postgres;

CREATE INDEX ix_date
  ON test
  USING btree
  (fdate);

CREATE INDEX ix_text
  ON test
  USING btree
  (ftext COLLATE pg_catalog."default");

将插入内容放入表格中.

数据是这样的:

152 2013-07-25 00:01:47 51e811712cfd6
100151  2013-07-25 00:28:25 51e825bfea275
101151  2013-07-25 00:29:26 51e825fcc5d94
153 2013-07-25 01:01:47 51e8117134c14
100152  2013-07-25 01:28:25 51e825bff1eb7
101152  2013-07-25 01:29:26 51e825fccd9e7
154 2013-07-25 02:01:47 51e811713d80d
100153  2013-07-25 02:28:25 51e825c0077c7
101153  2013-07-25 02:29:26 51e825fcd561a
155 2013-07-25 03:01:47 51e811716ffb2
100154  2013-07-25 03:28:25 51e825c013225
101154  2013-07-25 03:29:26 51e825fcdd243
156 2013-07-25 04:01:47 51e8117179af0
100155  2013-07-25 04:28:25 51e825c01cd74
101155  2013-07-25 04:29:26 51e825fce3f1c

在每个表中插入102 000行.

平均插入时间:

MysqL: 0.0328167504 сек.
Pgsql: 0.0183281872 сек.
- Pgsql is ~twice faster.

然后我选择:

select * from test 
where `fdate` > "2013-07-25" and `fdate` < "2013-08-21" 
order by `fdate`

(在FOR循环(1000)中选择,然后计算平均时间.)

MysqL: 0.0004650463 сек., 1944 rows
Pgsql: 0.0139540959 сек., 1944 rows
- Pgsql by 30! times more slowly.

为什么?

Pgsql EXPLAIN(ANALYZE,BUFFERS):

"Index Scan using ix_date on test  (cost=0.00..36.86 rows=780 width=30) (actual time=0.018..4.672 rows=1944 loops=1)"
"  Index Cond: ((fdate > '2013-07-25 00:00:00'::timestamp without time zone) AND (fdate < '2013-08-21 00:00:00'::timestamp without time zone))"
"  Buffers: shared hit=1954"
"Total runtime: 7.594 ms"

MysqL EXPLAIN:

1   SIMPLE  test    range   ix_date ix_date 8       1942    Using where

分析VERBOSE测试(Pgsql):

INFO:  analyzing "public.test"
INFO:  "test": scanned 750 of 750 pages, containing 102000 live rows and 0 dead rows; 30000 rows in sample, 102000 estimated total rows

解决方法:

重复的查询命中了MysqL query cache,我认为这个功能在Postgresql中不存在.

disabling the query cache之后重复测试,或者将SQL_NO_CACHE指令添加MysqL查询中(SELECT sql_NO_CACHE * FROM test …).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐