From 7cbff48f5782d1f7f844678e6b785aeb419b0c47 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Mon, 1 Dec 2014 11:59:12 +0100 Subject: [PATCH 18/30] Optimize: Skip value condition on invalid flags. HandlerHelper::itemWithFlagsCount gets called quite often apparently and I noticed that it was relatively slow from the Query Debugger in Akonadi Console. EXPLAIN'ing the query showed that it was using a slow-path for the WHERE FOO AND (BAR OR ASDF) condition. Here, ASDF was always id = -1, the id of the $IGNORED flag, which I apparently don't have. Getting rid of that condition simplifies the query to WHERE FOO AND BAR, which is apparently much better optimizable. Before, the query often showed a runtime of ~15ms. Now it is down to ~9ms. REVIEW: 121306 --- server/src/handlerhelper.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/src/handlerhelper.cpp b/server/src/handlerhelper.cpp index 634a26c..82347b4 100644 --- a/server/src/handlerhelper.cpp +++ b/server/src/handlerhelper.cpp @@ -123,6 +123,10 @@ int HandlerHelper::itemWithFlagsCount( const Collection &col, const QStringList // it hits an in-memory cache. Q_FOREACH ( const QString &flag, flags ) { const Flag f = Flag::retrieveByName( flag ); + if (!f.isValid()) { + // since we OR this condition, we can skip invalid flags to speed up the query + continue; + } cond.addValueCondition( PimItemFlagRelation::rightFullColumnName(), Query::Equals, f.id() ); } qb.addCondition( cond ); -- 2.1.0