CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • Scalability (Moderator: Donald Lobo) »
  • Increase Use of Indices
Pages: [1]

Author Topic: Increase Use of Indices  (Read 2042 times)

matthewboh

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 2
Increase Use of Indices
August 25, 2010, 01:20:04 pm
I'm starting to use some databases with 500K + records and while checking out some of the data loads I performed, noticed that some of the queries that are built force MySQL to read through each record.

One great example is in the Search Builder section.  If you select certain fields, it forces the search criteria to all lower case, then forces the database field to LOWER which then causes the query to read each record.  For example:

Code: [Select]
SELECT DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name  FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id  WHERE  (  ( LOWER(civicrm_state_province.name) = 'maryland' AND contact_a.contact_type IN ('Individual') )  )  AND (contact_a.is_deleted = 0) ;
Takes about 18 seconds to run against 500K records while

Code: [Select]
SELECT DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name  FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id  WHERE  (  ( civicrm_state_province.name = 'maryland' AND contact_a.contact_type IN ('Individual') )  )  AND (contact_a.is_deleted = 0) ;
Takes about 2 seconds and returns the same values.  I've also tried it with 'MARYLAND' and 'Maryland' and 'MaryLand' - all with the same results.

I'm also looking at the Advanced Search - I have a feeling that there might be a few more...

I'm very new to trying to add back to open source projects, so please feel free to let me know the correct way of doing things!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Increase Use of Indices
August 25, 2010, 02:03:55 pm
Hi,

The query builder is a nice tool, but as you said, generates not optimal queries.

In general, generating queries automatically means you will have some cases that won't be good (recently a discussion on advanced searches on related contacts).

If you know php, you can certainly dig into the code and see how it could be improved. You might want to write more unit tests to run automatically (eg with different cases).

Good luck, that part of the code is not the easiest to follow for what I have experienced.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Increase Use of Indices
August 25, 2010, 02:33:32 pm

how about this query

Code: [Select]
SELECT SQL_NO_CACHE DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name  FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id  WHERE  (  ( civicrm_state_province.name LIKE 'maryland%' AND contact_a.contact_type IN ('Individual') )  )  AND (contact_a.is_deleted = 0) ;


lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

matthewboh

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 2
Re: Increase Use of Indices
August 25, 2010, 02:37:42 pm
That uses the index as well - it took 2.73 seconds for 504K records

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Increase Use of Indices
August 25, 2010, 02:45:06 pm

In general for something like search builder giving the user a lot more options makes it a lot more flexible. it also makes the code a lot harder to write/debug/understand

not sure what direction we should be going for search builder. It can get incredibly complex and maybe the views2 integration does most of what it does?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • Scalability (Moderator: Donald Lobo) »
  • Increase Use of Indices

This forum was archived on 2017-11-26.