Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have noticed that FORCE INDEX helps when you have multiple joins and sub-queries on VARCHAR fields where both the FK and the referenced value are not the primary key, while at the same time having where clause on a DATE field.</p> <p>Something like:</p> <pre><code>SELECT NAME, a.reference_no, i.value, p.value FROM customers AS c INNER JOIN accounts AS a ON c.id = a.customer_id INNER JOIN invoices AS i ON i.reference_no = a.reference_no INNER JOIN payments AS p ON p.invoice_no = i.invoice_no WHERE payments.date &gt;= '2011-09-01' AND DATE &lt; '2011-10-01'; </code></pre> <p>mysql will always use the PKs and FKs, where you would it to use the payment_date index on the payments table first as it is the largest one. So a <code>FORCE INDEX(payment_date)</code> on the payments table join would help a lot.</p> <p>This an example from the third-party billing database that we use at work. We had huge issues with optimisation, and FORCE INDEX did the job most of the times. Usually we found the slow quires with mysqladmin, tested them with FORCE INDEX and send them to the vendors to rewrite them in the source code of the app.</p> <p>Here are the four tables to get a better grip on the example:</p> <pre><code>CREATE TABLE `customers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; CREATE TABLE `accounts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `customer_id` int(11) NOT NULL, `reference_no` varchar(10) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `reference_no_uniq` (`reference_no`), KEY `FK_accounts` (`customer_id`), CONSTRAINT `FK_accounts` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; CREATE TABLE `invoices` ( `id` int(11) NOT NULL AUTO_INCREMENT, `reference_no` varchar(10) NOT NULL, `invoice_no` varchar(10) NOT NULL, `value` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `invoice_no_uniq` (`invoice_no`), KEY `FK_invoices` (`reference_no`), CONSTRAINT `FK_invoices` FOREIGN KEY (`reference_no`) REFERENCES `accounts` (`reference_no`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; CREATE TABLE `payments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `invoice_no` varchar(10) NOT NULL, `value` int(11) NOT NULL, `date` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_payments` (`invoice_no`), KEY `payment_date` (`date`), CONSTRAINT `FK_payments` FOREIGN KEY (`invoice_no`) REFERENCES `invoices` (`invoice_no`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload