Answers.Body: <p>Do you perhaps have turned on 'Detailed Logging'? If so, turn it off at </p>
  
  <p>Administer -> System Settings -> Misc</p>
  
  <p>See also:</p>
  
  <p><a href="https://docs.civicrm.org/sysadmin/en/latest/setup/logging/" rel="nofollow noreferrer">https://docs.civicrm.org/sysadmin/en/latest/setup/logging/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T16:35:55.683Z
Answers.Text: Thanks for your suggestion. We do have logging turned on, but the organisation use this feature so if possible I would rather not turn it off.
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T16:03:54.293Z
Answers.Text: I think your answer would definitely improve the timings so I have marked it as correct.
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T16:31:51.250Z
Answers.LastActivityDate: 2019-04-01T16:31:51.250Z
Answers.UserDisplayName: Michael
Body: <p>Several times a day on our Civi site (CiviCRM 5.11.0 with Drupal 7.64) we are experiencing the following error:</p>
  
  <blockquote>
    <p>PDOException: SQLSTATE[HY000] [1203] User xxx already has more than
    'max_user_connections' active connections in lock_may_be_available()
    (line 167 of /includes/lock.inc).</p>
  </blockquote>
  
  <p>The error causes the site to crash and lasts for about a minute or two.</p>
  
  <p>We are on a shared hosting platform, and I contacted support about this. They gave us the following information about the issue:</p>
  
  <blockquote>
    <p><em>You have a limit of 40 connections per user which is being exceeded. The problem is that your database is not optimized, and 95% of the
    queries to it take an average of eight seconds to be executed. Each
    query goes through more than 12 billion rows. This is causing
    connections to overlap, hence the error.</em></p>
  </blockquote>
  
  <p>They suggested I should <em>'optimise the database'</em>. I am not completely sure what I should do to optimise the database. Can anyone give me any advice on what I can do to help prevent this error?</p>
  
  <p>The support people also sent me an excerpt from the MySQL slow query log:</p>
  
  <pre><code># Current date: Mon Apr 1 17:20:26 2019
  # Overall: 18.13k total, 902 unique, 0.13 QPS, 0.32x concurrency _________
  # Time range: 2019-03-31T00:27:16 to 2019-04-01T17:20:06
  # Attribute total min max avg 95% stddev median
  # ============ ======= ======= ======= ======= ======= ======= =======
  # Exec time 45692s 100ms 52s 3s 8s 3s 1s
  # Lock time 16s 0 4s 857us 366us 28ms 108us
  # Rows sent 28.16M 0 3.17M 1.59k 964.41 28.34k 964.41
  # Rows examine 12.25G 0 3.18M 708.38k 2.15M 753.22k 362.29k
  # Rows affecte 4.70M 0 1.23M 271.74 0.99 12.94k 0
  # Bytes sent 15.78G 11 2.63G 912.46k 830.38k 23.41M 790.84k
  # Query size 323.12M 6 4.15M 18.25k 1.86k 140.56k 51.63
  
  # Query 1: 0.08 QPS, 0.31x concurrency, ID 0x1859D41021C2B8A4 at byte 62429572
  # This item is included in the report because it matches --limit.
  # Scores: V/M = 1.44
  # Time range: 2019-03-31T01:01:42 to 2019-04-01T17:20:06
  # Attribute pct total min max avg 95% stddev median
  # ============ === ======= ======= ======= ======= ======= ======= =======
  # Count 59 10837
  # Exec time 94 43287s 100ms 16s 4s 8s 2s 4s
  # Lock time 20 3s 50us 51ms 294us 176us 2ms 113us
  # Rows sent 36 10.33M 0 1000 999.72 964.41 15.04 964.41
  # Rows examine 96 11.86G 19.53k 2.85M 1.12M 2.26M 694.00k 1.03M
  # Rows affecte 0 0 0 0 0 0 0 0
  # Bytes sent 54 8.59G 2.15k 846.53k 831.20k 830.38k 23.42k 790.84k
  # Query size 0 555.76k 51 53 52.51 51.63 0.45 51.63
  # String:
  # Databases wfto_civicrm
  # Hosts localhost
  # Last errno 0
  # Users wfto
  # Query_time distribution
  # 1us
  # 10us
  # 100us
  # 1ms
  # 10ms
  # 100ms ########
  # 1s ################################################################
  # 10s+ #
  # Tables
  # SHOW TABLE STATUS FROM `wfto_civicrm` LIKE 'log_civicrm_group'
  # SHOW CREATE TABLE `wfto_civicrm`.`log_civicrm_group`
  # EXPLAIN /*!50100 PARTITIONS*/
  SELECT * FROM `log_civicrm_group` LIMIT 2047000, 1000
  
  # Query 2: 0.01 QPS, 0.01x concurrency, ID 0x9BE31CBEA5AB2481 at byte 154842157
  # This item is included in the report because it matches --limit.
  # Scores: V/M = 0.09
  # Time range: 2019-03-31T00:59:00 to 2019-04-01T16:23:24
  # Attribute pct total min max avg 95% stddev median
  # ============ === ======= ======= ======= ======= ======= ======= =======
  # Count 9 1726
  # Exec time 1 772s 100ms 1s 447ms 777ms 202ms 433ms
  # Lock time 3 533ms 60us 50ms 308us 204us 2ms 119us
  # Rows sent 5 1.64M 0 1000 996.96 964.41 51.88 964.41
  # Rows examine 2 361.70M 31.25k 381.79k 214.59k 362.29k 95.30k 211.82k
  # Rows affecte 0 0 0 0 0 0 0 0
  # Bytes sent 2 387.79M 3.55k 276.83k 230.07k 233.54k 17.06k 222.42k
  # Query size 0 99.19k 58 59 58.85 56.92 0 56.92
  # String:
  # Databases wfto_civicrm
  # Hosts localhost
  # Last errno 0
  # Users wfto
  # Query_time distribution
  # 1us
  # 10us
  # 100us
  # 1ms
  # 10ms
  # 100ms ################################################################
  # 1s #
  # 10s+
  # Tables
  # SHOW TABLE STATUS FROM `wfto_civicrm` LIKE 'log_civicrm_custom_field'
  # SHOW CREATE TABLE `wfto_civicrm`.`log_civicrm_custom_field`
  # EXPLAIN /*!50100 PARTITIONS*/
  SELECT * FROM `log_civicrm_custom_field` LIMIT 367000, 1000
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-01T16:54:03.730Z
Comments.Text: Can you add a few stats about your system?  How many contacts, contributions, activities, events etc?  Do you make much use of smart groups?  Are you using custom ACL's?  What extensions are you running? Is it unusually big in any way?  Is it an old system that has been upgraded many times, or a fairly new install?  Just trying to get a sense of what sort of system this is.
Comments.UserDisplayName: Aidan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-02T07:32:28.770Z
Comments.Text: It is not that big a system. There are only about 3000 contacts, no contributions, minimal activities and events and no ACLs. We do however have about 300 custom fields. There are no particularly resource hungry extensions. We are using civirules, mosaico and fast action links.
Comments.UserDisplayName: Ben
Tags: <error><mysql><performance>
Title: Error: more than 'max_user_connections' active connections
ThreadText: # Error: more than 'max_user_connections' active connections
  
  (Asked by: username_0 on 2019-04-01)
  
  Several times a day on our Civi site (CiviCRM 5.11.0 with Drupal 7.64) we are experiencing the following error:
  
  PDOException: SQLSTATE[HY000] [1203] User xxx already has more than
    'max_user_connections' active connections in lock_may_be_available()
    (line 167 of /includes/lock.inc).
  
  The error causes the site to crash and lasts for about a minute or two.
  We are on a shared hosting platform, and I contacted support about this. They gave us the following information about the issue:
  
  You have a limit of 40 connections per user which is being exceeded. The problem is that your database is not optimized, and 95% of the
    queries to it take an average of eight seconds to be executed. Each
    query goes through more than 12 billion rows. This is causing
    connections to overlap, hence the error.
  
  They suggested I should 'optimise the database'. I am not completely sure what I should do to optimise the database. Can anyone give me any advice on what I can do to help prevent this error?
  The support people also sent me an excerpt from the MySQL slow query log:
  # Current date: Mon Apr 1 17:20:26 2019
  # Overall: 18.13k total, 902 unique, 0.13 QPS, 0.32x concurrency _________
  # Time range: 2019-03-31T00:27:16 to 2019-04-01T17:20:06
  # Attribute total min max avg 95% stddev median
  # ============ ======= ======= ======= ======= ======= ======= =======
  # Exec time 45692s 100ms 52s 3s 8s 3s 1s
  # Lock time 16s 0 4s 857us 366us 28ms 108us
  # Rows sent 28.16M 0 3.17M 1.59k 964.41 28.34k 964.41
  # Rows examine 12.25G 0 3.18M 708.38k 2.15M 753.22k 362.29k
  # Rows affecte 4.70M 0 1.23M 271.74 0.99 12.94k 0
  # Bytes sent 15.78G 11 2.63G 912.46k 830.38k 23.41M 790.84k
  # Query size 323.12M 6 4.15M 18.25k 1.86k 140.56k 51.63
  
  # Query 1: 0.08 QPS, 0.31x concurrency, ID 0x1859D41021C2B8A4 at byte 62429572
  # This item is included in the report because it matches --limit.
  # Scores: V/M = 1.44
  # Time range: 2019-03-31T01:01:42 to 2019-04-01T17:20:06
  # Attribute pct total min max avg 95% stddev median
  # ============ === ======= ======= ======= ======= ======= ======= =======
  # Count 59 10837
  # Exec time 94 43287s 100ms 16s 4s 8s 2s 4s
  # Lock time 20 3s 50us 51ms 294us 176us 2ms 113us
  # Rows sent 36 10.33M 0 1000 999.72 964.41 15.04 964.41
  # Rows examine 96 11.86G 19.53k 2.85M 1.12M 2.26M 694.00k 1.03M
  # Rows affecte 0 0 0 0 0 0 0 0
  # Bytes sent 54 8.59G 2.15k 846.53k 831.20k 830.38k 23.42k 790.84k
  # Query size 0 555.76k 51 53 52.51 51.63 0.45 51.63
  # String:
  # Databases wfto_civicrm
  # Hosts localhost
  # Last errno 0
  # Users wfto
  # Query_time distribution
  # 1us
  # 10us
  # 100us
  # 1ms
  # 10ms
  # 100ms ########
  # 1s ################################################################
  # 10s+ #
  # Tables
  # SHOW TABLE STATUS FROM `wfto_civicrm` LIKE 'log_civicrm_group'
  # SHOW CREATE TABLE `wfto_civicrm`.`log_civicrm_group`
  # EXPLAIN /*!50100 PARTITIONS*/
  SELECT * FROM `log_civicrm_group` LIMIT 2047000, 1000
  
  # Query 2: 0.01 QPS, 0.01x concurrency, ID 0x9BE31CBEA5AB2481 at byte 154842157
  # This item is included in the report because it matches --limit.
  # Scores: V/M = 0.09
  # Time range: 2019-03-31T00:59:00 to 2019-04-01T16:23:24
  # Attribute pct total min max avg 95% stddev median
  # ============ === ======= ======= ======= ======= ======= ======= =======
  # Count 9 1726
  # Exec time 1 772s 100ms 1s 447ms 777ms 202ms 433ms
  # Lock time 3 533ms 60us 50ms 308us 204us 2ms 119us
  # Rows sent 5 1.64M 0 1000 996.96 964.41 51.88 964.41
  # Rows examine 2 361.70M 31.25k 381.79k 214.59k 362.29k 95.30k 211.82k
  # Rows affecte 0 0 0 0 0 0 0 0
  # Bytes sent 2 387.79M 3.55k 276.83k 230.07k 233.54k 17.06k 222.42k
  # Query size 0 99.19k 58 59 58.85 56.92 0 56.92
  # String:
  # Databases wfto_civicrm
  # Hosts localhost
  # Last errno 0
  # Users wfto
  # Query_time distribution
  # 1us
  # 10us
  # 100us
  # 1ms
  # 10ms
  # 100ms ################################################################
  # 1s #
  # 10s+
  # Tables
  # SHOW TABLE STATUS FROM `wfto_civicrm` LIKE 'log_civicrm_custom_field'
  # SHOW CREATE TABLE `wfto_civicrm`.`log_civicrm_custom_field`
  # EXPLAIN /*!50100 PARTITIONS*/
  SELECT * FROM `log_civicrm_custom_field` LIMIT 367000, 1000
  
  **Tags:** error, mysql, performance
  
  > **Comments:**
  > **username_1**: Can you add a few stats about your system?  How many contacts, contributions, activities, events etc?  Do you make much use of smart groups?  Are you using custom ACL's?  What extensions are you running? Is it unusually big in any way?  Is it an old system that has been upgraded many times, or a fairly new install?  Just trying to get a sense of what sort of system this is.  
  > **username_0**: It is not that big a system. There are only about 3000 contacts, no contributions, minimal activities and events and no ACLs. We do however have about 300 custom fields. There are no particularly resource hungry extensions. We are using civirules, mosaico and fast action links.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-04-01. Score: 1 | Accepted answer
  
  Do you perhaps have turned on 'Detailed Logging'? If so, turn it off at
  
  Administer -> System Settings -> Misc
  
  See also:
  
  <https://docs.civicrm.org/sysadmin/en/latest/setup/logging/>
  
  > **Comments:**
  > **username_0**: Thanks for your suggestion. We do have logging turned on, but the organisation use this feature so if possible I would rather not turn it off.  
  > **username_0**: I think your answer would definitely improve the timings so I have marked it as correct.  
  
  ---
  
  