PSQL分组与汇总速度

时间:2014-06-08 16:18:04

标签: psql

所以,一般的问题是,在GROUP BY子句中获取字段的聚合或具有额外的表达式会更快。以下是两个查询。

查询1(GROUP BY中的额外表达式):

SELECT sum(subquery.what_i_want)
           FROM (
             SELECT table_1.some_id,
               (
                 CASE WHEN some_date_field IS NOT NULL
                   THEN
                     FLOOR(((some_date_field - current_date)::numeric / 7) + 1) * MAX(some_other_integer)
                   ELSE
                     some_integer * MAX(some_other_integer)
                 END
               ) what_i_want
             FROM table_1
             JOIN table_2 on table_1.some_id = table_2.id
             WHERE ((some_date_field IS NOT NULL AND some_date_field > current_date) OR some_integer > 0) -- per the data and what i want, one of these will always be true
             GROUP BY some_id_1, some_date_field, some_integer
           ) subquery

查询2(使用(任意,因为这里的表2字段的每条记录都具有相同的值(在此数据集中))聚合函数):

SELECT sum(subquery.what_i_want)
           FROM (
             SELECT table_1.some_id,
               (
                 CASE WHEN MAX(some_date_field) IS NOT NULL
                   THEN
                     FLOOR(((MAX(some_date_field) - current_date)::numeric / 7) + 1) * MAX(some_other_integer)
                   ELSE
                     MAX(some_integer) * MAX(some_other_integer)
                 END
               ) what_i_want
             FROM table_1
             JOIN table_2 on table_1.some_id = table_2.id
             WHERE ((some_date_field IS NOT NULL AND some_date_field > current_date) OR some_integer > 0) -- per the data and what i want, one of these will always be true
             GROUP BY some_id_1
           ) subquery

据我所知,psql没有提供良好的基准测试工具。 \timing on只有一次查询的次数,因此运行基准测试并进行足够的试验以获得有意义的结果最多也是乏味的。

为了记录,我确实在大约n = 50时这样做,并且看到聚合方法(查询2)平均运行得更快,但p值为〜.13,所以不太确定。

' sup那个?

1 个答案:

答案 0 :(得分:0)

一般答案 - 应该+ - 相同。当在字段上使用/不使用函数时,有机会点击/错过基于函数的索引,但是没有聚合函数,而在列表列表中的where子句中。但这只是猜测。

use for analyzing execution EXPLAIN ANALYZE的内容是psql。在计划中,您不仅可以查看扫描类型,还可以查看迭代次数,成本和单个操作时间。当然,您可以将其与File "/usr/local/lib/python2.7/site-packages/keras/__init__.py", line 3, in <module> from . import activations File "/usr/local/lib/python2.7/site-packages/keras/activations.py", line 3, in <module> from . import backend as K File "/usr/local/lib/python2.7/site-packages/keras/backend/__init__.py", line 73, in <module> from .tensorflow_backend import * File "/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1, in <module> import tensorflow as tf File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module> from tensorflow.python import * File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 51, in <module> from tensorflow.python import pywrap_tensorflow File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module> raise ImportError(msg) ImportError: Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module> from tensorflow.python.pywrap_tensorflow_internal import * File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module> _pywrap_tensorflow_internal = swig_import_helper() File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) ImportError: dlopen(/usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 10): Library not loaded: @rpath/libcublas.8.0.dylib Referenced from: /usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so Reason: image not found Failed to load the native TensorFlow runtime.

一起使用