Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql optimization
    primarykey
    data
    text
    <p>I am building a site with lots and lots of filters. Every time you click/narrow down your search, all counters get recalculated for all filters. I've realized that I have created somewhat of a problem here because on every click, I am recounting all filters by doing about ~50 count queries. This will create quite a significant database load when there are many users using the site at once..</p> <p>The filter counters look like this in case you want more context: <a href="http://screencast.com/t/3bxrWTAtm" rel="nofollow">http://screencast.com/t/3bxrWTAtm</a></p> <p>What is the best way to optimize these counts? I could use memcache but that would have to be cached for about 15-30 mins max as new listings could show up. </p> <p>Here is an example of one of the COUNT queries: </p> <pre><code>SELECT COUNT(id) AS numOfRows FROM (SELECT `id`, `theLatitude`, `theLongitude`, ( 3959 * acos( cos( radians(45.5086699) ) * cos( radians( theLatitude ) ) * cos( radians( theLongitude ) - radians(-73.5539925) ) + sin( radians(45.5086699) ) * sin( radians( theLatitude ) ) ) ) AS distance FROM `housing` WHERE (`elevator` = '1') AND ( `property-type` = 'loft' ) HAVING distance &lt; 75) AS `newid` </code></pre> <p>Housing table looks like this:</p> <pre><code>CREATE TABLE IF NOT EXISTS `housing` ( `id` varchar(10) NOT NULL, `date` datetime NOT NULL, `user` varchar(20) NOT NULL, `email` varchar(50) NOT NULL, `title` varchar(60) NOT NULL, `address` varchar(120) DEFAULT NULL, `formatted_address` varchar(120) NOT NULL, `theLatitude` double DEFAULT NULL, `theLongitude` double DEFAULT NULL, `property-type` varchar(20) NOT NULL, `square-feet` int(9) DEFAULT NULL, `bathrooms` double NOT NULL DEFAULT '0', `bedrooms` int(2) NOT NULL DEFAULT '0', `price` int(10) DEFAULT NULL, `priceUSD` int(10) NOT NULL, `currency` varchar(3) DEFAULT NULL, `period` varchar(5) DEFAULT NULL, `lease-terms` varchar(20) NOT NULL, `available-from` date NOT NULL, `contactName` varchar(30) NOT NULL, `contactPhone1` varchar(30) NOT NULL, `contactPhone2` varchar(30) NOT NULL, `description` text NOT NULL, `cats` tinyint(1) NOT NULL DEFAULT '0', `small-dogs` tinyint(1) NOT NULL DEFAULT '0', `small-pets` tinyint(1) NOT NULL DEFAULT '0', `big-dogs` tinyint(1) NOT NULL DEFAULT '0', `alarm-system` tinyint(1) NOT NULL DEFAULT '0', `air-conditioning` tinyint(1) NOT NULL DEFAULT '0', `balcony` tinyint(1) NOT NULL DEFAULT '0', `carpet-floors` tinyint(1) NOT NULL DEFAULT '0', `dishwasher` tinyint(1) NOT NULL DEFAULT '0', `electricity` tinyint(1) NOT NULL DEFAULT '0', `fireplace` tinyint(1) NOT NULL DEFAULT '0', `furniture` tinyint(1) NOT NULL DEFAULT '0', `heating` tinyint(1) NOT NULL DEFAULT '0', `high-ceilings` tinyint(1) NOT NULL DEFAULT '0', `hot-water` tinyint(1) NOT NULL DEFAULT '0', `natural-gas` tinyint(1) NOT NULL DEFAULT '0', `spa-hot-tub` tinyint(1) NOT NULL DEFAULT '0', `refrigerator` tinyint(1) NOT NULL DEFAULT '0', `stove` tinyint(1) NOT NULL DEFAULT '0', `storage-space` tinyint(1) NOT NULL DEFAULT '0', `walk-in-closets` tinyint(1) NOT NULL DEFAULT '0', `washer-dryer` tinyint(1) NOT NULL DEFAULT '0', `doorman` tinyint(1) NOT NULL DEFAULT '0', `elevator` tinyint(1) NOT NULL DEFAULT '0', `health-facilities` tinyint(1) NOT NULL DEFAULT '0', `inside-parking` tinyint(1) NOT NULL DEFAULT '0', `laundry-facilities` tinyint(1) NOT NULL DEFAULT '0', `outside-parking` tinyint(1) NOT NULL DEFAULT '0', `pool-sauna` tinyint(1) NOT NULL DEFAULT '0', `wheelchair-access` tinyint(1) NOT NULL DEFAULT '0', `authnumber` varchar(15) NOT NULL, `validated` tinyint(1) NOT NULL DEFAULT '0', </code></pre> <p>PRIMARY KEY (<code>id</code>) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;</p> <p>There are 50+ fields that a user can filter by.</p> <p>Anything to point me in the right direction would be greatly appreciated.</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.
 

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