server icon indicating copy to clipboard operation
server copied to clipboard

Use FTS for contact search instead of LIKE

Open fladi opened this issue 6 years ago • 10 comments

We are experiencing long delays when selecting contacts to create new shares for. Searching takes about 20-30 seconds after the user has finished typing.

Our installation runs Nextcloud 16 with PHP 7.3 and MariaDB 10.3. We have 22k rows in oc_cards and 140k rows in oc_cards_properties.

Testing the queries created by OCA\DAV\CardDAV\CardDavBackend->search manually showed that each one takes about 4 seconds to complete, like this one:

SELECT `c`.`carddata`, `c`.`uri` FROM `oc_cards` `c` WHERE `c`.`id` IN (SELECT DISTINCT `cp`.`cardid` FROM `oc_cards_properties` `cp` WHERE (`cp`.`addressbookid` = 'XXX') AND ((`cp`.`name` = 'EMAIL') OR (`cp`.`name` = 'FN')) AND (`cp`.`value`  COLLATE utf8mb4_general_ci LIKE '%pattern%'));

As LIKE statements can only make use of indizes if they do not start with a % wildcard, each query has to to a full table scan. I tried to compare this with a FTS index on the oc_cards_properties.value field and using MATCH ... AGAINST ... instead of LIKE:

CREATE FULLTEXT INDEX oc_cards_properties_name_FTS ON nextcloud.oc_cards_properties (value);
SELECT `c`.`carddata`, `c`.`uri` FROM `oc_cards` `c` WHERE `c`.`id` IN (SELECT DISTINCT `cp`.`cardid` FROM `oc_cards_properties` `cp` WHERE (`cp`.`addressbookid` = 'XXX') AND ((`cp`.`name` = 'EMAIL') OR (`cp`.`name` = 'FN')) AND (MATCH(`cp`.`value`) AGAINST('pattern')));

Now the query took only ~220ms instead of 4000ms.

Would you consider switching to Full-Text-Search, at least for MySQL/MariaDB? Large installations like ours would certainly benefit from it. Doctrine support for MATCH is available in https://github.com/beberlei/DoctrineExtensions.

fladi avatar Jan 17 '20 20:01 fladi

Thanks for reporting :+1:

Probably this patch https://github.com/nextcloud/server/pull/18816 could also improve the lookup speed?

kesselb avatar Jan 17 '20 21:01 kesselb

@kesselb I don't think so, the patch in #18816 only limits the number of results for a search but in the worst case (number of rows matching is lower than the limit) still results in a full table scan without indices.

I have now patched my local installation to use FTS and the search times have gone down from 30 seconds to 1 second. I'm willing to contribute my changes but I'm not that familiar with writing tests in PHP so I would need someone to guide me on this.

fladi avatar Jan 18 '20 19:01 fladi

cc @juliushaertl @nickvergessen

kesselb avatar Jan 18 '20 20:01 kesselb

@fladi could you please somehow share your patch. I think we are facing the same problem. We' ve got about 70k LDAP users and searching takes about 10-20 seconds. Reducing it to 1 second would be a great result for us. In our case one query takes about 8 seconds.

# Query_time: 8.536125  Lock_time: 0.000076  Rows_sent: 2  Rows_examined: 433223
# Rows_affected: 0  Bytes_sent: 19316
SELECT c.carddata, c.uri FROM oc_cards c WHERE c.id IN (SELECT DISTINCT cp.cardid FROM oc_cards_properties cp WHERE (cp.addressbookid = '1') AND ((cp.name = 'EMAIL') OR (cp.name = 'FN')) AND (cp.value  COLLATE utf8mb4_general_ci LIKE '%Petrov Ivan%'));

lostmap avatar Feb 03 '20 08:02 lostmap

Maybe we can also do some other improvements to reduce the complexity of the query. E.g. when there is a space in the search string, we don't even need to check for an email address anymore. :thinking:

nickvergessen avatar Feb 06 '20 21:02 nickvergessen

@lostmap I just pushed my changes from my local fork: https://github.com/fladi/server/commit/e2a0f2f3ae90a90cdf2270c89406ed1495c2c1d3

You will need to create the FTS index by hand as described in the commit message.

@nickvergessen I do not think that this will help that much as the fundamental problem is LIKE being unable to use indices. Which, at large tables, will always lead to slow queries.

fladi avatar Feb 24 '20 13:02 fladi

I do not think that this will help that much as the fundamental problem is LIKE being unable to use indices.

The thing is if we don't run the query at all anymore, the query is a lot faster ;)

nickvergessen avatar Feb 24 '20 13:02 nickvergessen

I do not think that this will help that much as the fundamental problem is LIKE being unable to use indices.

The thing is if we don't run the query at all anymore, the query is a lot faster ;)

I would like to confirm that our install (120k+ useri) with this patch reduces 15-25 second wait time on the same query to less than 3 seconds (as that is what we have reporting in the MySQL slow log).

macleajb avatar May 25 '20 12:05 macleajb

@MorrisJobke May I ask how #21222 relates to the usage of FTS? I do not consider this PR to warrant closing this issue.

fladi avatar Jul 06 '20 17:07 fladi

it's not realted to FTS, but #21222 and #21221 together solve the performance issue that lead to this issue in first place which is why we think the issue is okay to be closed for now

nickvergessen avatar Jul 06 '20 17:07 nickvergessen