PostgreSQL timestamp字段统计误区

前端之家收集整理的这篇文章主要介绍了PostgreSQL timestamp字段统计误区前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
开发人员说两个类似的sql统计出来的结果差别很大,对此不是很理解,还原一下场景及分析处理方案

OS:CentOS 5
DB:Postgres 9.2.4

1.数据准备
  1. [postgres@localhost ~]$ psql
  2. psql (9.2.4)
  3. Type "help" for help.
  4.  
  5. postgres=# create table t_kenyon(id serial,ctime timestamp without time zone,mtime date,remark text);
  6. NOTICE: CREATE TABLE will create implicit sequence "t_kenyon_id_seq" for serial column "t_kenyon.id"
  7. CREATE TABLE
  8. postgres=# insert into t_kenyon(ctime,mtime,remark) select generate_series('2013-04-22'::date,'2013-04-24'::date,'1h'),generate_series('2013-04-22'::date,'1d'),'Kenyon_Good_Boy!';
  9. INSERT 0 147
  10. postgres=# select * from t_kenyon limit 10;
  11. id | ctime | mtime | remark
  12. -----+---------------------+------------+------------------
  13. 736 | 2013-04-22 00:00:00 | 2013-04-22 | Kenyon_Good_Boy!
  14. 737 | 2013-04-22 01:00:00 | 2013-04-23 | Kenyon_Good_Boy!
  15. 738 | 2013-04-22 02:00:00 | 2013-04-24 | Kenyon_Good_Boy!
  16. 739 | 2013-04-22 03:00:00 | 2013-04-22 | Kenyon_Good_Boy!
  17. 740 | 2013-04-22 04:00:00 | 2013-04-23 | Kenyon_Good_Boy!
  18. 741 | 2013-04-22 05:00:00 | 2013-04-24 | Kenyon_Good_Boy!
  19. 742 | 2013-04-22 06:00:00 | 2013-04-22 | Kenyon_Good_Boy!
  20. 743 | 2013-04-22 07:00:00 | 2013-04-23 | Kenyon_Good_Boy!
  21. 744 | 2013-04-22 08:00:00 | 2013-04-24 | Kenyon_Good_Boy!
  22. 745 | 2013-04-22 09:00:00 | 2013-04-22 | Kenyon_Good_Boy!
  23. (10 rows)
2.统计sql
  1. postgres=# select count(1) from t_kenyon where ctime>'2013-04-22';
  2. count
  3. -------
  4. 144
  5. (1 row)
  6.  
  7. postgres=# select count(1) from t_kenyon where ctime>='2013-04-23';
  8. count
  9. -------
  10. 75
  11. (1 row)
  12.  
  13. postgres=# select count(1) from t_kenyon where mtime>='2013-04-23';
  14. count
  15. -------
  16. 98
  17. (1 row)
  18.  
  19. postgres=# select count(1) from t_kenyon where mtime>'2013-04-22';
  20. count
  21. -------
  22. 98
  23. (1 row)
3.分析
同事觉得第二个字段统计是正常,第一个是非正常的,表示不解,但是仔细观察一下,这两个字段类型是不一样的,ctime是timestamp类型,mtime是date类型,当条件是ctime>'2013-04-22'时其实是等价于ctime>='2013-04-22 00:00:00',所以会取到2013-04-22 01:00:00这些数据,但是ctime>='2013-04-23'时,其实是等价于ctime>='2013-04-23 00:00:00'或者ctime>'2013-04-22 23:59:59',所以两者有很大的出入,但是date类型的就不受此影响了。
例子如下:
  1. postgres=# select count(1) from t_kenyon where ctime>'2013-04-22 23:59:59';
  2. count
  3. -------
  4. 75
  5. (1 row)
  6.  
  7. postgres=# select count(1) from t_kenyon where ctime>='2013-04-23 00:00:00';
  8. count
  9. -------
  10. 75
  11. (1 row)
  12.  
  13. postgres=# select count(1) from t_kenyon where ctime>'2013-04-22 00:00:00';
  14. count
  15. -------
  16. 144
  17. (1 row)
  18.  
  19. postgres=# select count(1) from t_kenyon where ctime>='2013-04-22 00:00:00';
  20. count
  21. -------
  22. 147
  23. (1 row)
  24.  
  25. postgres=# select count(1) from t_kenyon where mtime>'2013-04-22';
  26. count
  27. -------
  28. 98
  29. (1 row)
  30.  
  31. postgres=# select count(1) from t_kenyon where mtime>'2013-04-22 10:00:00';
  32. count
  33. -------
  34. 98
  35. (1 row)
  36.  
  37. postgres=# select count(1) from t_kenyon where mtime>='2013-04-23';
  38. count
  39. -------
  40. 98
  41. (1 row)
  42.  
  43. postgres=# select count(1) from t_kenyon where mtime>='2013-04-23 13:00:00';
  44. count
  45. -------
  46. 98
  47. (1 row)
所以统计时需要注意一下。

猜你在找的Postgre SQL相关文章