Contributed by Calpont, InfiniDB Community Edition is an open source, scale-up analytics database engine for your data warehousing, business intelligence and read-intensive application needs. Enabled via MySQLTM and purpose-built for an analytical workload with column-oriented technology at its core, the multi-threaded capabilities of InfiniDB Community Edition fully encompass query, transactional support and bulk load operations.  So come on in, grab a download and get started.

             | 

Welcome, Guest
Username Password: Remember me

Conditions in aggregate/sum functions
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Conditions in aggregate/sum functions

Conditions in aggregate/sum functions 1 year, 11 months ago #491

  • rasel200209
  • OFFLINE
  • Senior Boarder
  • Posts: 66
  • Karma: 0
we use many forms of this...

SUM( (case when (year = '2009' and week<=40
OR year = '2008' and week>=1) then 1 else 0
end) * myvalue ) as TYEAR

so there is a condition in the Sum function.

can infini enhance the speed where there are Case in the Sum?
Last Edit: 1 year, 11 months ago by rasel200209.

Re:Conditions in aggregate/sum functions 1 year, 10 months ago #492

  • jtommaney
  • OFFLINE
  • Senior Boarder
  • Posts: 49
  • Karma: 4
The cost of the operation is likely related to the # of qualifying rows * the number of operations. You may be able to reduce the time slightly if you can simplify the number of steps in the expression. Here I have a compound date field I can express as a between clause rather than the compound logic show below.

mysql> select  sum(case when lo_orderdate between 19920101 and 19920630
    -> then 1 else 0 end  * lo_extendedprice) 1992_Q1_Q2_sum, count(*)
    -> from lineorder where lo_orderdate <= 19921231;
+--------------------+----------+
| 1992_Q1_Q2_sum     | count(*) |
+--------------------+----------+
| 138804076164817.00 | 91248844 |
+--------------------+----------+
1 row in set (5.90 sec)

mysql>
mysql>
mysql> select  sum(case when (year = 1992 and month <= 6 ) and (year = 1992 and month >= 1)
    -> then 1 else 0 end * lo_extendedprice) 1992_Q1_Q2_sum, count(*)
    -> from lineorder where lo_orderdate <= 19921231;
+--------------------+----------+
| 1992_Q1_Q2_sum     | count(*) |
+--------------------+----------+
| 138804076164817.00 | 91248844 |
+--------------------+----------+
1 row in set (7.81 sec)

mysql> select  sum(lo_extendedprice) 1992_Q1_Q2_sum, count(*)
    -> from lineorder where lo_orderdate <= 19921231;
+--------------------+----------+
| 1992_Q1_Q2_sum     | count(*) |
+--------------------+----------+
| 279113147753549.00 | 91248844 |
+--------------------+----------+
1 row in set (2.29 sec)

mysql> select  sum(1 * lo_extendedprice) 1992_Q1_Q2_sum, count(*)   from lineorder where lo_orderdate <= 19921231;
+--------------------+----------+
| 1992_Q1_Q2_sum     | count(*) |
+--------------------+----------+
| 279113147753549.00 | 91248844 |
+--------------------+----------+
1 row in set (4.54 sec)


There is definitely some overhead for inclusion of even a trivial function expression.
I'm not sure what else to recommend for you without access to the data model/queries.

Thanks - Jim Tommaney
Chief Product Architect
  • Page:
  • 1
Time to create page: 0.27 seconds