Answers.Body: <p>EDIT: This answer is wrong. See Jon's answer. The reserved rules don't use the values in the database they use custom queries.</p>
  
  <hr>
  
  <p>If you have access to the database type</p>
  
  <p><code>SELECT * from civicrm_dedupe_rule r inner join civicrm_dedupe_rule_group rg on rg.id = r.dedupe_rule_group_id;</code></p>
  
  <p>which will give you a table which isn't pretty but is mostly understandable.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-04T18:19:32.273Z
Answers.Text: Ah you're right. I'll update answer.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-04T20:22:40.910Z
Answers.Text: Thanks that tells me what I need, so I have deleted my earlier comment on your previous version of the answer
Answers.UserDisplayName: Mick Kahn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T13:38:32.293Z
Answers.Text: I'm taking off the green tick because Deremit has marked the answer as wrong, but (see below), I'm not offering the tick to Jon's answer yet as it doesn't fully answer my question.
Answers.UserDisplayName: Mick Kahn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-04T17:35:12.313Z
Answers.LastActivityDate: 2019-04-05T12:29:49.840Z
Answers.LastEditDate: 2019-04-05T12:29:49.840Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>Demerit's and Mick's answers are incorrect for the (built-in) reserved rules - though it's definitely confusing!</p>
  
  <p>If a RuleGroup has a value in the <code>name</code> field, and that name corresponds to a filename in <a href="https://github.com/civicrm/civicrm-core/tree/master/CRM/Dedupe/BAO/QueryBuilder" rel="nofollow noreferrer">CRM/Dedupe/BAO/QueryBuilder</a>, then the customized SQL in those files will be used. The existing entries in <code>civicrm_rule</code> for those RuleGroups are holdovers from before that system existed, and editing them has no effect.</p>
  
  <p>"Standard" dedupe rules with multiple criteria are very inefficient compared to handwritten SQL, which is why this is a valuable technique.  You can create your own handwritten queries with <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_dupeQuery/" rel="nofollow noreferrer">hook_civicrm_dedupe</a>, and the <a href="https://github.com/veda-consulting/uk.co.vedaconsulting.module.dedupe/blob/master/dedupe.php#L259" rel="nofollow noreferrer">Veda dedupe</a> extension has a number of excellent examples.  Note that this extension doesn't work on modern Civi because of some of its other functions, but the dedupe rules can be ripped out into something else.</p>
  
  <p>Finally - I learned just yesterday that the built-in handwritten dedupe rules seem to execute different SQL when comparing in Unsupervised/Supervised mode (a single contact) vs. General mode (find all dupes).  While I haven't proved it, I suspect that if you're in the rare scenario of needing to optimize your unsupervised/supervised dedupes, creating a new class to extend <code>CRM_Dedupe_BAO_QueryBuilder</code> is the way to go.  I just posted <a href="https://github.com/MegaphoneJon/org.agbu.optimizeddedupe" rel="nofollow noreferrer">org.agbu.optimizeddedupe</a> to provide an example of this.</p>
  
  <p>UPDATE: More clarification.</p>
  
  <p>To understand how the queries work, it's best to look at an example, eg <a href="https://github.com/civicrm/civicrm-core/blob/master/CRM/Dedupe/BAO/QueryBuilder/IndividualSupervised.php" rel="nofollow noreferrer">IndividualUnsupervised.php</a>.</p>
  
  <p>The <code>internal</code> function is used if you go to <strong>Contacts » Find and Merge Duplicate Contacts</strong> and click <strong>Use Rule</strong>.  The SQL query is:</p>
  
  
  
  <pre><code>            SELECT contact1.id as id1, contact2.id as id2, {$rg-&gt;threshold} as weight
              FROM civicrm_contact as contact1
                JOIN civicrm_email as email1 ON email1.contact_id=contact1.id
                JOIN civicrm_contact as contact2 ON
                  contact1.first_name = contact2.first_name AND
                  contact1.last_name = contact2.last_name
                JOIN civicrm_email as email2 ON
                  email2.contact_id=contact2.id AND
                  email1.email=email2.email
              WHERE contact1.contact_type = 'Individual'"
  </code></pre>
  
  <p>First, note that the weight is set to <code>$rg-&gt;threshold</code> - that is, the threshold in <code>civicrm_rule_group</code>.  In other words, if this SQL matches, these records automatically meet the threshold for that rule.  Hopefully that answers your main question!  If you remove that field, you can run this SQL as-is in a SQL client and get a complete list of the duplicates it would return.</p>
  
  <p>To further clarify - unlike "regular" rules which are the result of several queries, each with their own weight - this runs a SINGLE query, and sets the weight equal to the rule's threshold.  So it's a straight yes/no answer whether a record is a duplicate, based on whether the SQL finds them.</p>
  
  <p>That's not to say that you can't simulate length/weight, but it's tricky.  My <code>org.agbu.optimizeddedupe</code> rule has a <a href="https://github.com/MegaphoneJon/org.agbu.optimizeddedupe/blob/master/CRM/Dedupe/BAO/QueryBuilder/OnlineDonations.php#L29-L42" rel="nofollow noreferrer">SQL statement</a> you can look at which gives the same results as this rule:</p>
  
  <p><a href="https://i.sstatic.net/Wdt0R.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Wdt0R.png" alt="enter image description here"></a></p>
  
  <p>However, it took about 5 seconds to compare even a single submitted contact against the existing 165,000 contacts in this databse with the existing rule.  Now it's almost instantaneous.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T08:45:54.247Z
Answers.Text: Jon G - that's all very interesting, but a bit beyond me. I don't think I need to optimize my own dedupe rules and I know I can make my own rules supervised or unsupervised and leave the prefigured rules unused as general. What I was trying to do was understand the weights/thresholds for the preconfigured rules. I don't see any values in the code etc that you have pointed to, so do they still come from the queries that Deremit has provided. They are certain;y compatible with the results that I see.
Answers.UserDisplayName: Mick Kahn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T14:00:06.433Z
Answers.Text: I'm still a bit confused as I'm not that good at reading the code, though I can work out enough see that these rules are treated a special case. I can't see any values for length, weight or threshold there so still don't know the actual criteria for considering contacts to be duplicate under these rules. Or is some different algorithm used. Given that I have a relatively small number of contacts, I can just ditch the pre-configured rules and use my own (less efficient) ones. But it would be nice to know the criteria used in order to understand why some duplicates have been created.
Answers.UserDisplayName: Mick Kahn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T16:20:33.993Z
Answers.Text: @MickKahn I just updated my answer, hopefully it makes things clearer!
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T22:26:59.613Z
Answers.Text: Thanks Jon - I don't understand it all, but see that my answer is not helpful so will delete that. I'm seeing some other unexpected results, but can acheive what I need with my own rules, so will move on to other things for now.
Answers.UserDisplayName: Mick Kahn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T02:58:12.997Z
Answers.LastActivityDate: 2019-04-05T16:20:12.163Z
Answers.LastEditDate: 2019-04-05T16:20:12.163Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I'm wanting to refine the de-duping rules, but first of all I'd like to find out exactly what the predefined rules are before I create my own. They are reserved, so you can't edit them, which is fine, but it only tells you which fields they use and not what the weights and thresholds are so the full behaviour is not clear.</p>
  
Tags: <duplicate-contacts>
Title: Reserved de-dupe rules
ThreadText: # Reserved de-dupe rules
  
  (Asked by: username_0 on 2019-04-04)
  
  I'm wanting to refine the de-duping rules, but first of all I'd like to find out exactly what the predefined rules are before I create my own. They are reserved, so you can't edit them, which is fine, but it only tells you which fields they use and not what the weights and thresholds are so the full behaviour is not clear.
  
  **Tags:** duplicate-contacts
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-04-05. Score: 4 | Accepted answer
  
  username_1's and Mick's answers are incorrect for the (built-in) reserved rules - though it's definitely confusing!
  If a RuleGroup has a value in the name field, and that name corresponds to a filename in CRM/Dedupe/BAO/QueryBuilder, then the customized SQL in those files will be used. The existing entries in civicrm_rule for those RuleGroups are holdovers from before that system existed, and editing them has no effect.
  "Standard" dedupe rules with multiple criteria are very inefficient compared to handwritten SQL, which is why this is a valuable technique.  You can create your own handwritten queries with hook_civicrm_dedupe, and the Veda dedupe extension has a number of excellent examples.  Note that this extension doesn't work on modern Civi because of some of its other functions, but the dedupe rules can be ripped out into something else.
  Finally - I learned just yesterday that the built-in handwritten dedupe rules seem to execute different SQL when comparing in Unsupervised/Supervised mode (a single contact) vs. General mode (find all dupes).  While I haven't proved it, I suspect that if you're in the rare scenario of needing to optimize your unsupervised/supervised dedupes, creating a new class to extend CRM_Dedupe_BAO_QueryBuilder is the way to go.  I just posted org.agbu.optimizeddedupe to provide an example of this.
  UPDATE: More clarification.
  To understand how the queries work, it's best to look at an example, eg IndividualUnsupervised.php.
  The internal function is used if you go to Contacts » Find and Merge Duplicate Contacts and click Use Rule.  The SQL query is:
              SELECT contact1.id as id1, contact2.id as id2, {$rg->threshold} as weight
              FROM civicrm_contact as contact1
                JOIN civicrm_email as email1 ON email1.contact_id=contact1.id
                JOIN civicrm_contact as contact2 ON
                  contact1.first_name = contact2.first_name AND
                  contact1.last_name = contact2.last_name
                JOIN civicrm_email as email2 ON
                  email2.contact_id=contact2.id AND
                  email1.email=email2.email
              WHERE contact1.contact_type = 'Individual'"
  
  First, note that the weight is set to $rg->threshold - that is, the threshold in civicrm_rule_group.  In other words, if this SQL matches, these records automatically meet the threshold for that rule.  Hopefully that answers your main question!  If you remove that field, you can run this SQL as-is in a SQL client and get a complete list of the duplicates it would return.
  To further clarify - unlike "regular" rules which are the result of several queries, each with their own weight - this runs a SINGLE query, and sets the weight equal to the rule's threshold.  So it's a straight yes/no answer whether a record is a duplicate, based on whether the SQL finds them.
  That's not to say that you can't simulate length/weight, but it's tricky.  My org.agbu.optimizeddedupe rule has a SQL statement you can look at which gives the same results as this rule:
  
  However, it took about 5 seconds to compare even a single submitted contact against the existing 165,000 contacts in this databse with the existing rule.  Now it's almost instantaneous.
  
  > **Comments:**
  > **username_0**: Jon G - that's all very interesting, but a bit beyond me. I don't think I need to optimize my own dedupe rules and I know I can make my own rules supervised or unsupervised and leave the prefigured rules unused as general. What I was trying to do was understand the weights/thresholds for the preconfigured rules. I don't see any values in the code etc that you have pointed to, so do they still come from the queries that Deremit has provided. They are certain;y compatible with the results that I see.  
  > **username_0**: I'm still a bit confused as I'm not that good at reading the code, though I can work out enough see that these rules are treated a special case. I can't see any values for length, weight or threshold there so still don't know the actual criteria for considering contacts to be duplicate under these rules. Or is some different algorithm used. Given that I have a relatively small number of contacts, I can just ditch the pre-configured rules and use my own (less efficient) ones. But it would be nice to know the criteria used in order to understand why some duplicates have been created.  
  > **username_2**: @MickKahn I just updated my answer, hopefully it makes things clearer!  
  > **username_0**: Thanks Jon - I don't understand it all, but see that my answer is not helpful so will delete that. I'm seeing some other unexpected results, but can acheive what I need with my own rules, so will move on to other things for now.  
  
  ---
  
  ### Answer by username_1 on 2019-04-04. Score: 2
  
  EDIT: This answer is wrong. See Jon's answer. The reserved rules don't use the values in the database they use custom queries.
  
  ---
  
  If you have access to the database type
  
  `SELECT * from civicrm_dedupe_rule r inner join civicrm_dedupe_rule_group rg on rg.id = r.dedupe_rule_group_id;`
  
  which will give you a table which isn't pretty but is mostly understandable.
  
  > **Comments:**
  > **username_1**: Ah you're right. I'll update answer.  
  > **username_0**: Thanks that tells me what I need, so I have deleted my earlier comment on your previous version of the answer  
  > **username_0**: I'm taking off the green tick because Deremit has marked the answer as wrong, but (see below), I'm not offering the tick to Jon's answer yet as it doesn't fully answer my question.  
  
  ---
  
  