My database query works very fast without using count query command. I use index and index works very well. It brings me data very fast. However, when I use something
SELECT COUNT(*) AS count … It works very slow. It lasts like 1 minute and there is no index for count. I have 415.000 records in my database.
What is the solution of this ? I think index does not work with a count ? Any advice please, how can I achieve this?
SELECT app.*, META(app).id AS id FROM app WHERE ( deleted = FALSE OR deleted IS MISSING ) AND _class = “com.myexample.app.device.data.model.DeviceEntity” AND appId = “something” AND dpnguage = “somelanguage” LIMIT 100 OFFSET 0
This query works very well and fast…Response time smaller than 100ms. However
SELECT COUNT(*) AS count FROM app WHERE ( deleted = FALSE OR deleted IS MISSING ) AND _class = “com.myexample.app.device.data.model.DeviceEntity” AND appId = “something” AND dpnguage = “somelanguage” LIMIT 100 OFFSET 0
This query works so slow. This is because of count command. Response time was like around 1 min.
I am doing query from couchbase UI. Normally I use CrudRepository in my Java/Spring application.
Indexes
#primary
class_appId_idx on ["_class","appId"]
class_appId_deleted_idx on ["_class","appId"] where ((deleted = false) or (deleted is missing))
First query you have pagination (LIMIT 100 OFFSET 0) without any ORDER. Query is finished when the limit condition is reached.
In second query you have the Aggregates without any group by. Query needs produce all qualified items and aggregate it. Eventually produces 1 item. It takes time
CREATE INDEX ix1 ON app(`_class`,`appId`, `dpnguage`) WHERE IFMISSING(deleted,false) = false;
SELECT COUNT(1) AS cnt FROM app
WHERE IFMISSING(deleted,false) = false AND
_class = "com.myexample.app.device.data.model.DeviceEntity" AND
`appId` = "something" AND dpnguage = "somelanguage";
SELECT *, META().id FROM app
WHERE IFMISSING(deleted,false) = false AND
_class = "com.myexample.app.device.data.model.DeviceEntity" AND
`appId` = "something" AND dpnguage = "somelanguage"
OFFSET 0
LIMIT 100;
SELECT COUNT(1) AS cnt FROM appconnect USE INDEX (ix1)
WHERE IFMISSING(deleted,false) = false AND
_class = "com.myexample.appconnect.device.data.model.DeviceEntity" AND
appId = "whitelabel" AND dp.language = "tr";
Hum. What version of couchbase and can you expand index definition and post the screen shot again.
You can do EXPLAIN on latest query and see if it only uses ix1 and uses covers