setup:
Ignition 7.2.2
historical chart. History database (postgres) has partitions of 5 to 8 million rows each. approx 1200 tags
Trying to track down the cause of trend performance issues.
If I use the trend chart with three pens the performance is ok. It’s running this query:
SELECT "tagid","intvalue","floatvalue","stringvalue","datevalue","t_stamp","dataintegrity" FROM sqlt_data_1_968 WHERE "t_stamp">=? and "t_stamp"<=? and ("tagid"=? OR "tagid"=? OR "tagid"=?) ORDER BY "t_stamp" ASC
If I add a forth pen. this query is run which usually times out.
SELECT "tagid","intvalue","floatvalue","stringvalue","datevalue","t_stamp","dataintegrity" FROM sqlt_data_1_968 WHERE "tagid" IN (?) and "t_stamp"<? ORDER BY "t_stamp" DESC LIMIT 1
Explain plan on the first shows a single index scan."Index Scan using sqlt_data_1_974t_stampndx on sqlt_data_1_974 (cost=0.00..9.00 rows=1 width=556)"
Explain plan on the second shows a reverse index scan following by a limit." -> Index Scan Backward using sqlt_data_1_974t_stampndx on sqlt_data_1_974 (cost=0.00..30.07 rows=7 width=556)"
*Note the big difference in the cost
Questions:
Why does the second query have a limit 1 clause?
Why is the query with more then 3 pens different?
Just for fun I added a forth ‘OR “tagid”=’ to the first query and got the same explain plan as the first query.
Conventional wisdom has it that the only query that in more expensive than IN is NOT IN. IN is certainly more expensive then a bunch of ORs