Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot really optimize a query with leading wildcards (even with <code>FULLTEXT</code> searches).</p> <p>The only thing you can do here is to split the query in three (on client side):</p> <pre><code>SELECT tblpart.partid,tblpart.title,tblcategory.category,tblheadcategory.headcategory FROM tblpart INNER JOIN tblcategory ON tblpart.categoryid = tblcategory.categoryid INNER JOIN tblheadcategory ON tblcategory.headcategoryid = tblheadcategory.headcategoryid WHERE tblpart.title = 'bmw' ORDER BY tblcategory.category LIKE '%bmw%' DESC LIMIT 50 SELECT tblpart.partid,tblpart.title,tblcategory.category,tblheadcategory.headcategory FROM tblpart INNER JOIN tblcategory ON tblpart.categoryid = tblcategory.categoryid INNER JOIN tblheadcategory ON tblcategory.headcategoryid = tblheadcategory.headcategoryid WHERE tblpart.title &lt;&gt; 'bmw' AND (tblpart.title LIKE '%bmw%' OR tblpart.description LIKE '%bmw%' OR tblpart.brand LIKE '%bmw%') AND tblcategory.category LIKE '%bmw%' LIMIT N SELECT tblpart.partid,tblpart.title,tblcategory.category,tblheadcategory.headcategory FROM tblpart INNER JOIN tblcategory ON tblpart.categoryid = tblcategory.categoryid INNER JOIN tblheadcategory ON tblcategory.headcategoryid = tblheadcategory.headcategoryid WHERE tblpart.title &lt;&gt; 'bmw' AND (tblpart.title LIKE '%bmw%' OR tblpart.description LIKE '%bmw%' OR tblpart.brand LIKE '%bmw%') AND tblcategory.category NOT LIKE '%bmw%' LIMIT N </code></pre> <p>and replace <code>N</code> in the last queries with <code>50 - records</code>, where <code>records</code> is the number of records returned by the previous queries</p> <p>The first query can be served with an index on <code>title</code>.</p> <p><strong>Update:</strong></p> <p>A <code>FULLTEXT</code> search can be implemented like this:</p> <pre><code>CREATE TABLE `tblpart_search` ( `partid` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `brand` varchar(100) DEFAULT NULL, `description` varchar(100) DEFAULT NULL, PRIMARY KEY (`partid`), FULLTEXT KEY `all` (`title`,`brand`,`description`) ) ENGINE=MyISAM AUTO_INCREMENT=359596 DEFAULT CHARSET=utf8; </code></pre> <p>Triggers:</p> <pre><code>DELIMITER ;; CREATE TRIGGER `tblpart_insert_trigger` AFTER INSERT ON `tblpart` FOR EACH ROW INSERT INTO tblpart_search VALUES(NEW.partid,NEW.title,NEW.brand,NEW.description);; DELIMITER ; DELIMITER ;; CREATE TRIGGER `tblpart_update_trigger` AFTER UPDATE ON `tblpart` FOR EACH ROW UPDATE tblpart_search SET tblpart_search.title=NEW.title,tblpart_search.brand=NEW.brand,tblpart_search.description=NEW.description WHERE tblpart_search.partid=NEW.partid;; DELIMITER ; DELIMITER ;; CREATE TRIGGER `tblpart_delete_trigger` AFTER DELETE ON `tblpart` FOR EACH ROW DELETE FROM tblpart_search WHERE tblpart_search.partid=OLD.partid;; DELIMITER ; </code></pre> <p>The new query:</p> <pre><code>SELECT tblpart.partid,tblpart.title,tblcategory.category,tblheadcategory.headcategory FROM tblpart_search INNER JOIN tblpart ON tblpart_search.partid = tblpart.partid INNER JOIN tblcategory ON tblpart.categoryid = tblcategory.categoryid INNER JOIN tblheadcategory ON tblcategory.headcategoryid = tblheadcategory.headcategoryid WHERE MATCH (tblpart_search.title, tblpart_search.brand, tblpart_search.description) AGAINST ('+bmw +car' IN BOOLEAN MODE) LIMIT 50; </code></pre> <p>Set <code>ft_min_word_len</code> to <code>3</code> or less so that it could index the <code>3</code>-character words like <code>'BMW'</code> and <code>'CAR'</code>.</p>
    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