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) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • killer query
Pages: [1] 2

Author Topic: killer query  (Read 2121 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
killer query
May 24, 2011, 07:39:55 pm
We have 2 sites we recently upgraded to 3.4.1 and both have recently fallen over due to problematic queries. I am not sure if this is new as we see a similar (from memory) query in the slow log on a 3.2 install and both sites are on a new server as well which may have made the symptoms worse.

However, Torrance was able to identify the slow query & to cut it down to the part that causes the problem.

I haven't figured out the source of the query but I have figured out what it would look like if it were to be gentle on the server

This query is an excerpt of the slow query and cannot complete on our server - explain statement suggests it is examining 633 million rows - 38000 * 15,000. Query ran for over 2 hours on customer server before we killed it (assume T is doing something so no query can run that long ever again)

Code: [Select]
SELECT count( DISTINCT ( civicrm_activity.id ) )  FROM civicrm_contact contact_a
LEFT JOIN civicrm_activity_target ON civicrm_activity_target.target_contact_id = contact_a.id
LEFT JOIN civicrm_activity
ON ( civicrm_activity.id = civicrm_activity_target.activity_id OR civicrm_activity.source_contact_id = contact_a.id )




This is the same query using a union (union gets rid of the duplicates according to the manual so I didn't use distinct but counted them after the duplicates - this one finishes happily in a second or so

Code: [Select]
SELECT count( a.id )
FROM (
(

SELECT civicrm_activity.id
FROM civicrm_contact contact_a
LEFT JOIN civicrm_activity_target ON civicrm_activity_target.target_contact_id = contact_a.id
LEFT JOIN civicrm_activity ON ( civicrm_activity.id = civicrm_activity_target.activity_id )
WHERE civicrm_activity.id IS NOT NULL
)
UNION (

SELECT civicrm_activity.id
FROM civicrm_contact contact_a
LEFT JOIN civicrm_activity_target ON civicrm_activity_target.target_contact_id = contact_a.id
LEFT JOIN civicrm_activity ON ( civicrm_activity.source_contact_id = contact_a.id )
WHERE civicrm_activity.id IS NOT NULL
)
) AS a
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

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: killer query
May 24, 2011, 09:47:08 pm

Can you find out where this query is being called from

thanx

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 24, 2011, 10:03:41 pm
I don't know what called the query here but the query is defined if the from function of activity bao

line 387 est

                $from .= " $side JOIN civicrm_activity
                                      ON ( ( ( civicrm_activity.id = civicrm_activity_assignment.activity_id )
                                               OR ( civicrm_activity.id = civicrm_activity_target.activity_id ) )


In this join the part after the OR will never use an index.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 24, 2011, 10:15:22 pm
Line 189 in CRM_Activity_Selector_Search suggests that it is the source object

        $this->_query->_distinctComponentClause = 'DISTINCT ( civicrm_activity.id )';
   
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 24, 2011, 10:33:21 pm
I think the bit about the distinct is a red herring

SELECT civicrm_activity.id  FROM civicrm_contact contact_a
LEFT JOIN civicrm_activity_target ON civicrm_activity_target.target_contact_id = contact_a.id
LEFT JOIN civicrm_activity
ON ( civicrm_activity.id = civicrm_activity_target.activity_id OR civicrm_activity.source_contact_id = contact_a.id )

is enough to crash our server if I run it against about 38000 contact with a av 3 activities

This is the main BAO function
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 24, 2011, 10:42:35 pm
OK, I lied - it didn't crash the server - after 10 minutes (621 seconds) I was overcome with guilt & the knowledge Pete might be reading this thread & killed the query
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: killer query
May 25, 2011, 12:55:28 am
So that's when doing a search on activities or the activity tab ?

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 25, 2011, 12:56:34 am
Well, the join that is slow is in the BAO query class in the from function - I expect the activity search is the think that we're being impacted by
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 25, 2011, 01:20:17 am
OK, just checked & doing a blank search from find activities causes the server to crash on this query
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 25, 2011, 01:52:06 am
Once again I lie - the server didn't crash - even though the query ran for 25 minutes before I remembered to go back & kill it (hope you're not reading Pete)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: killer query
May 25, 2011, 02:02:33 am
Hold on, you don't have timeout on your php ? is the mysql query still running the the calling script timesout ?

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 25, 2011, 02:04:37 am
yeah should do shouldn't we - I'll get Torrance to put one in!

I still think that a query that examines 6 million rows just cos a user clicked on a search is pretty problematic
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: killer query
May 25, 2011, 02:30:22 am
Quote from: Eileen on May 25, 2011, 02:04:37 am
I still think that a query that examines 6 million rows just cos a user clicked on a search is pretty problematic

Wouldn't argue on that one.  Wondering if there is a "proper" way of identifying these queries before it hits us on the prod. Eg if we run the test suite with a big table and see if they are slow queries in the log (but would probably need weeks to run...)

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

torrance123

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 3
  • CiviCRM version: 4.0
  • CMS version: Drupal 7
  • MySQL version: 5.0.91
  • PHP version: 5.3.3
Re: killer query
May 25, 2011, 03:18:52 pm
Regarding having max_execution_time set in php.ini:

Quote
The configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. [from php.net]

So, unfortunately, this script is running regardless of any max_execution_time set in php.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: killer query
May 25, 2011, 05:52:50 pm
But, you can set a max execution time (or similar) in SQL can't you?
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • killer query

This forum was archived on 2017-11-26.