Multi column searching - mysql

I have real estate website. Currently my table looks like this
CREATE TABLE `properties` (
`property_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`property_type` int(11) NOT NULL,
`seller_id` int(11) NOT NULL,
`seller_type` int(11) NOT NULL,
`transaction_type` tinyint(4) NOT NULL DEFAULT '1',
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`price` int(10) unsigned NOT NULL,
`area` int(11) NOT NULL,
`bedrooms` tinyint(4) NOT NULL,
`bathrooms` tinyint(4) NOT NULL,
`city` varchar(30) NOT NULL,
`location` varchar(30) NOT NULL,
`listing_images` text NOT NULL,
`list_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`activated` tinyint(4) NOT NULL DEFAULT '0',
`property_status` tinyint(4) NOT NULL DEFAULT '0',
`reviewed` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`property_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1
In the search properties page, there is a form that allows users to filter by property type,seller type, maximum price, area, max & min bedrooms,bathrooms etc to narrow it. I tried to optimize it by creating index on each field, but it is very slow. What is the correct way to optimize this?

Related

mysql 'slow queries log' return a huge number of rows examined but explain seems ok

The sql query
# Query_time: 16.536276 Lock_time: 0.000159 Rows_sent: 756 Rows_examined: 8392194
SET timestamp=1555422526;
SELECT c.id AS c__id, c.company_id AS c__company_id,
c.ordinary_price AS c__ordinary_price, c.nights AS c__nights,
c.food_type AS c__food_type, c.period AS c__period,
c.period_desc AS c__period_desc, c.extra AS c__extra,
c.coupons_bought AS c__coupons_bought, c.coupon_price AS c__coupon_price,
c.coordinates AS c__coordinates, c.best_price AS c__best_price,
c.from_price AS c__from_price, c.end_datetime AS c__end_datetime,
c.hide_clock AS c__hide_clock, c.hide_discount AS c__hide_discount,
c.booking_hotel_id AS c__booking_hotel_id, c.title AS c__title,
c.option_people AS c__option_people, c.option_room AS c__option_room,
c.option_period AS c__option_period, c.city AS c__city,
c2.id AS c2__id, c2.people AS c2__people, c2.room AS c2__room,
( SELECT c8.url AS c8__url
FROM campaign_images c8
WHERE (c8.campaign_id = c.id
AND c8.photo_type = 'list')
ORDER BY c8.ordering ASC LIMIT 1
) AS c__0
FROM campaign c
LEFT JOIN campaign_options c2 ON c.id = c2.campaign_id
AND (c2.active = 1)
LEFT JOIN city_in_campaign c3 ON c.id = c3.campaign_id
LEFT JOIN city c4 ON c3.city_id = c4.id
LEFT JOIN company c5 ON c.company_id = c5.id
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
LEFT JOIN campaign_in_group c7 ON c.id = c7.campaign_id
WHERE c.id IN ('13308', '13281', '13265')
AND (c.status IN ('published')
AND c.start_datetime <= NOW()
AND c.end_datetime >= NOW()
AND c5.id = '2111'
AND c.id != '14624'
AND (c7.group_id in (1)
OR c7.group_id is NULL
)
)
ORDER BY c.coupon_expire_datetime ASC;
create table campaign
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`source_id` int(11) DEFAULT NULL,
`secondary_source_id` int(11) DEFAULT NULL,
`source_hotel_id` int(11) DEFAULT NULL,
`booking_hotel_id` int(11) DEFAULT NULL,
`booking_board_id` int(11) DEFAULT NULL,
`booking_rate` varchar(100) DEFAULT NULL,
`ordinary_price` decimal(9,2) NOT NULL,
`best_price` tinyint(1) NOT NULL DEFAULT '0',
`from_price` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(10) DEFAULT 'draft',
`type` varchar(20) DEFAULT 'other',
`deal_status` varchar(20) DEFAULT 'in_progress',
`coupon_price` decimal(9,2) NOT NULL,
`cosmote_discount` decimal(5,2) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '0',
`period` varchar(255) DEFAULT NULL,
`period_desc` varchar(1000) DEFAULT NULL,
`hide_period` tinyint(1) NOT NULL DEFAULT '0',
`food_type` varchar(100) DEFAULT NULL,
`stars` varchar(100) DEFAULT NULL,
`adults` tinyint(1) DEFAULT NULL,
`childs` tinyint(1) DEFAULT NULL,
`extra` varchar(255) DEFAULT NULL,
`best_point` varchar(511) DEFAULT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_desc` varchar(500) DEFAULT NULL,
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`family_packages_desc` varchar(1000) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`start_datetime` datetime DEFAULT NULL,
`end_datetime` datetime DEFAULT NULL,
`coupon_expire_datetime` datetime DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`city` varchar(255) DEFAULT NULL,
`min_coupons` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(4) NOT NULL DEFAULT '0',
`hide_clock` tinyint(1) NOT NULL DEFAULT '0',
`hide_discount` tinyint(1) NOT NULL DEFAULT '0',
`hide_purchases` tinyint(1) NOT NULL DEFAULT '0',
`booking_enabled` tinyint(1) NOT NULL DEFAULT '0',
`booking_phone` varchar(50) DEFAULT NULL,
`refresh` tinyint(1) NOT NULL DEFAULT '0',
`installments` tinyint(1) NOT NULL DEFAULT '1',
`receipt` enum('0','1') NOT NULL DEFAULT '0',
`newsletters_sent` tinyint(4) NOT NULL DEFAULT '0',
`max_coupons` int(11) NOT NULL DEFAULT '0',
`max_coupons_per_user` int(11) DEFAULT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`fake_orders` int(11) DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`newsletter_title` varchar(500) DEFAULT NULL,
`linkwise_title` varchar(500) DEFAULT NULL,
`option_title` varchar(255) DEFAULT NULL,
`option_title_en` varchar(255) DEFAULT NULL,
`option_people` varchar(150) DEFAULT NULL,
`option_room` varchar(150) DEFAULT NULL,
`option_period` varchar(150) DEFAULT NULL,
`name` varchar(1200) DEFAULT NULL,
`description` text,
`highlights` text,
`coupon_instructions` text,
`show_in_recent_deals` tinyint(4) NOT NULL DEFAULT '1',
`youtube_video_id` varchar(100) DEFAULT NULL,
`in_side` tinyint(4) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`send_newsletter` tinyint(4) NOT NULL DEFAULT '1',
`resend_newsletter` tinyint(4) NOT NULL DEFAULT '0',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`ordering` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `company_id_idx` (`company_id`),
KEY `user_id_idx` (`user_id`),
KEY `status_indx` (`status`),
KEY `str_dt_indx` (`start_datetime`),
KEY `end_dt_indx` (`end_datetime`),
KEY `side_indx` (`in_side`),
KEY `ord_indx` (`ordering`),
KEY `global_indx` (`is_global`),
KEY `act_indx` (`active`),
KEY `coup_expr_index` (`coupon_expire_datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=14788 DEFAULT CHARSET=utf8
create table campaign_options
CREATE TABLE `campaign_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`people` varchar(100) DEFAULT NULL,
`room` varchar(100) DEFAULT NULL,
`food` varchar(100) DEFAULT NULL,
`period` varchar(100) DEFAULT NULL,
`coupon_price` decimal(9,2) NOT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) DEFAULT '0.00',
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`main` tinyint(1) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `active_indx` (`active`),
KEY `family_indx` (`family`),
KEY `main_indx` (`main`)
) ENGINE=InnoDB AUTO_INCREMENT=48990 DEFAULT CHARSET=utf8
create table city_in_campaign
CREATE TABLE `city_in_campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `city_id_idx` (`city_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=227176 DEFAULT CHARSET=utf8
create table city
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`is_active` tinyint(3) unsigned DEFAULT NULL,
`modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `country_id_idx` (`country_id`),
KEY `is_active_indx` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8
create table company
CREATE TABLE `company` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`ref_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`description` text,
`logo` varchar(255) DEFAULT NULL,
`phone_number` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
`skype_name` varchar(50) DEFAULT NULL,
`icq_number` varchar(255) DEFAULT NULL,
`payment_information` text,
`extra1` text,
`extra2` text,
`extra3` text,
`video` varchar(500) DEFAULT NULL,
`checked` tinyint(1) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `checked_indx` (`checked`),
KEY `ordering_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=2519 DEFAULT CHARSET=utf8
create table campaign_in_category
CREATE TABLE `campaign_in_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `category_id_idx` (`category_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=457080 DEFAULT CHARSET=utf8
create table campaign_in_group
CREATE TABLE `campaign_in_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`created_datetime` datetime DEFAULT NULL,
`modified_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `camp_group_indx` (`campaign_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1175 DEFAULT CHARSET=utf8
campaign total rows: 12,657
campaign_options total rows: 43,714
city_in_campaign total rows: 15,162
city total rows: 215
company total rows: 1,756
campaign_in_category total rows: 38,817
campaign_in_group total rows: 395
explain of the query
which ones have index
it looks you need composite index because file sorting.
usage:
CREATE INDEX index_name
ON table_name(c2,c3,c4);
You are getting above result because some time o/p of explain gives different result as response of query of prod.
For optimization of query you can't have a fix answer. it varies from case to case. As for this situation I think you need to perform performance testing of this query. You can do this by inserting some records in all referenced table and then checking it's performance using explain.
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
seems to be totally useless. But the Optimizer may not realize it. Remove it and any other dead code.

I can't login to my e-shop based on Prestashop 1.7. (No ps_employee table in DB.)

I can't find ps_employee table in the DB.
Is it possible that this table has just dissappeard?
How can I create new one and add new admin account?
Of course is not possible this table can disappear by itself, anyway you can create it together with the associated _shop table:
CREATE TABLE IF NOT EXISTS `ps_employee` (
`id_employee` int(10) unsigned NOT NULL auto_increment,
`id_profile` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL DEFAULT '0',
`lastname` varchar(32) NOT NULL,
`firstname` varchar(32) NOT NULL,
`email` varchar(128) NOT NULL,
`passwd` varchar(60) NOT NULL,
`last_passwd_gen` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`stats_date_from` date DEFAULT NULL,
`stats_date_to` date DEFAULT NULL,
`stats_compare_from` date DEFAULT NULL,
`stats_compare_to` date DEFAULT NULL,
`stats_compare_option` int(1) unsigned NOT NULL DEFAULT 1,
`preselect_date_range` varchar(32) DEFAULT NULL,
`bo_color` varchar(32) DEFAULT NULL,
`bo_theme` varchar(32) DEFAULT NULL,
`bo_css` varchar(64) DEFAULT NULL,
`default_tab` int(10) unsigned NOT NULL DEFAULT '0',
`bo_width` int(10) unsigned NOT NULL DEFAULT '0',
`bo_menu` tinyint(1) NOT NULL DEFAULT '1',
`active` tinyint(1) unsigned NOT NULL DEFAULT '0',
`optin` tinyint(1) unsigned NOT NULL DEFAULT '1',
`id_last_order` int(10) unsigned NOT NULL DEFAULT '0',
`id_last_customer_message` int(10) unsigned NOT NULL DEFAULT '0',
`id_last_customer` int(10) unsigned NOT NULL DEFAULT '0',
`last_connection_date` date DEFAULT NULL,
`reset_password_token` varchar(40) DEFAULT NULL,
`reset_password_validity` datetime DEFAULT NULL,
PRIMARY KEY (`id_employee`),
KEY `employee_login` (`email`,`passwd`),
KEY `id_employee_passwd` (`id_employee`,`passwd`),
KEY `id_profile` (`id_profile`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION;
CREATE TABLE IF NOT EXISTS `ps_employee_shop` (
`id_employee` INT(11) UNSIGNED NOT NULL,
`id_shop` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id_employee`,`id_shop`),
KEY `id_shop` (`id_shop`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION;
Now only need add a record with your account details, in the passwd you can add whatever text, because you will need recover your password in the admin login page.

MYSQL : How can we optimize the select query to fetch more than 1 million data using date range

I have a table containing more than 1 million data and I am trying to fetch using a date range. I have indexed the date column and still its searching the entire rows. Can someone give a best solution to fix this up.
/** Table Structure **/
CREATE TABLE `claim_history` (
`claim_history_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`consultation_id` varchar(50) NOT NULL,
`member_id` int(11) unsigned DEFAULT NULL,
`card_number` char(18) DEFAULT NULL,
`member_name` varchar(200) DEFAULT NULL,
`network_code` int(11) unsigned NOT NULL DEFAULT '0',
`mobile_number` varchar(50) DEFAULT NULL,
`soap_number` varchar(50) DEFAULT NULL,
`diagnosis_id` varchar(50) DEFAULT NULL,
`diagnosis_code` varchar(50) DEFAULT NULL,
`diagnosis_description` text,
`activity_id` int(11) unsigned DEFAULT NULL,
`activity_code` varchar(50) DEFAULT NULL,
`activity_description` text,
`activity_comments` text,
`activity_quantity` int(11) unsigned NOT NULL DEFAULT '0',
`is_erx` tinyint(1) NOT NULL DEFAULT '0',
`lab_id` int(11) unsigned DEFAULT NULL,
`lab_name` varchar(100) DEFAULT NULL,
`session_no` tinyint(1) unsigned NOT NULL DEFAULT '0',
`net_amount` decimal(10,2) DEFAULT NULL,
`copay_pct` decimal(10,2) DEFAULT NULL,
`copay_amt` decimal(10,2) DEFAULT NULL,
`total_cost` decimal(10,2) DEFAULT NULL,
`is_edited` tinyint(1) unsigned DEFAULT '0',
`edit_comments` text,
`is_assigned_mcc` tinyint(1) DEFAULT '0'
`mcc_user` int(11) unsigned DEFAULT NULL,
`mcc_user_name` varchar(50) DEFAULT NULL,
`rule_name` varchar(200) DEFAULT NULL,
`mcu_assigned_time` timestamp NULL DEFAULT NULL,
`mcu_action_performed_time` timestamp NULL DEFAULT NULL,
`mcu_open_time` timestamp NULL DEFAULT NULL,
`is_reappealed` tinyint(1) unsigned NOT NULL DEFAULT '0',
`reappeal_count` int(11) unsigned NOT NULL,
`denial_code` varchar(50) DEFAULT NULL,
`denial_description` text,
`rejection_comments` text,
`justification_comment` text,
`justification_reply` text,
`justification_count` tinyint(1) unsigned NOT NULL DEFAULT '0',
`edit_comment_reply` text,
`is_referral` tinyint(1) unsigned NOT NULL DEFAULT '0',
`is_physiotherapy` tinyint(1) unsigned NOT NULL DEFAULT '0',
`facility_id` int(11) unsigned DEFAULT NULL,
`provider_code` varchar(50) DEFAULT NULL,
`provider_name` varchar(100) DEFAULT NULL,
`doctor_user_id` int(11) DEFAULT NULL,
`doctor_name` varchar(100) DEFAULT NULL,
`referral_doctor_name` varchar(100) DEFAULT NULL,
`referral_clinic_name` varchar(100) DEFAULT NULL,
`pic_id` int(11) unsigned DEFAULT NULL,
`pic_name` varchar(100) DEFAULT NULL,
`ig_id` int(11) unsigned DEFAULT NULL,
`ig_name` varchar(100) DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`history_created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`history_last_modified_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) unsigned NOT NULL DEFAULT '0',
`last_modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_by` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`claim_history_id`),
KEY `idx_consultation_id` (`consultation_id`),
KEY `idx_card_number` (`member_id`,`card_number`),
KEY `idx_provider` (`facility_id`),
KEY `idx_soap_number` (`soap_number`),
KEY `idx_created_on` (`created_on`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
/** SQL QUERY **/
SELECT fields FROM claim_history
WHERE created_on BETWEEN '2017-01-01 00:00:00'
AND '2017-05-01 00:00:00';
The query you provided has a syntax error.
Dependent on the range you put into your query, the query optimiser might use the index or not.
Try a really short range like one day and then use one year, you'll probably already see the difference.
Check this thread for more details:
MySQL ignores my index on a timestamp column

#1060 - Duplicate column name 'ID' Why?

CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
`Kofferraum` varchar(50) NOT NULL DEFAULT '0|0|0|0|',
`Typ` int(11) NOT NULL,
`Tuning` varchar(255) NOT NULL,
`Spawnpos_X` varchar(50) NOT NULL,
`Spawnpos_Y` varchar(50) NOT NULL,
`Spawnpos_Z` varchar(50) NOT NULL,
`Spawnrot_X` varchar(50) NOT NULL,
`Spawnrot_Y` varchar(50) NOT NULL,
`Spawnrot_Z` varchar(50) NOT NULL,
`Farbe` varchar(50) NOT NULL,
`Paintjob` varchar(50) NOT NULL DEFAULT '3',
`Benzin` varchar(50) NOT NULL DEFAULT '100',
`Slot` float NOT NULL,
`Special` int(11) NOT NULL DEFAULT '0',
`Lights` varchar(50) NOT NULL DEFAULT '|255|255|255|',
`Distance` double NOT NULL DEFAULT '0',
`STuning` varchar(50) NOT NULL DEFAULT '0|0|0|0|0|0|',
`AuktionsID` int(10) NOT NULL DEFAULT '0',
`GangVehicle` tinyint(1) NOT NULL DEFAULT '0',
`rc` int(1) NOT NULL DEFAULT '0',
`spezcolor` varchar(50) NOT NULL DEFAULT '|0|0|0|0|0|0|',
`Sportmotor` int(1) NOT NULL DEFAULT '0',
`Bremse` varchar(1) NOT NULL DEFAULT '0',
`Antrieb` varchar(10) NOT NULL,
`plate` text NOT NULL,
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Because you're using 'id' twice. Either remove the duplicate or change the name of this to something else:
ID int(11) NOT NULL
You have the column id twice. Once at the beginning an one at the end.
CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
...
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
...
remove one of them

Mysql query optimization - its really slow

I have next log.
Please somebody explain me where is the problem.
My table deals is 142906 why is it taking so much time?
# Query_time: 5.524629 Lock_time: 0.000059 Rows_sent: 3 Rows_examined: 142906
SET timestamp=1381963341;
SELECT *,1 as distance FROM deals
WHERE end_date > '1381959736'
GROUP BY title
ORDER BY COALESCE(distance, 999999999) , distance ASC LIMIT 0 , 3;
Here is ddl.
CREATE TABLE `deals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sku` varchar(180) NOT NULL DEFAULT '',
`price` decimal(8,2) DEFAULT NULL,
`retail_price` decimal(8,2) DEFAULT NULL,
`category` int(1) DEFAULT '4',
`advertiser` varchar(120) DEFAULT NULL,
`image_url` varchar(255) DEFAULT NULL,
`tiks` int(5) DEFAULT NULL,
`buy_url` varchar(255) DEFAULT NULL,
`description` text,
`views` int(6) NOT NULL DEFAULT '1',
`title` varchar(100) DEFAULT NULL,
`brand` varchar(100) DEFAULT NULL,
`api` varchar(50) DEFAULT NULL,
`discount` int(2) DEFAULT NULL,
`black_list` smallint(1) DEFAULT '0',
`gender` tinyint(1) DEFAULT NULL,
`sort` tinyint(1) DEFAULT NULL,
`is_home` tinyint(1) DEFAULT NULL,
`is_quick_shop` tinyint(1) DEFAULT NULL,
`date_add` int(11) DEFAULT NULL,
`is_sale` tinyint(1) DEFAULT NULL,
`is_new` tinyint(1) DEFAULT NULL,
`is_brand_show` tinyint(1) DEFAULT NULL,
`date_modified` int(11) NOT NULL DEFAULT '0',
`modifier_id` smallint(2) DEFAULT NULL,
`modified` smallint(1) DEFAULT NULL,
`longitute` decimal(10,8) DEFAULT NULL,
`latitute` decimal(10,8) DEFAULT NULL,
`is_deal` tinyint(1) DEFAULT NULL,
`best_seller` tinyint(1) DEFAULT NULL,
`home_cat` int(2) DEFAULT NULL,
`end_date` int(11) DEFAULT NULL,
`temp_category` varchar(120) DEFAULT NULL,
`update_cron` smallint(1) unsigned DEFAULT '1',
`alias` varchar(120) DEFAULT NULL,
`found_by` text,
`fb_share` smallint(3) DEFAULT NULL,
`tw_share` smallint(3) DEFAULT NULL,
`pin_share` smallint(3) DEFAULT NULL,
`google_share` smallint(3) DEFAULT NULL,
`last_tiked` int(11) DEFAULT NULL,
`is_simple` smallint(1) DEFAULT '0',
`dealer_id` int(3) DEFAULT NULL,
`clicks` int(5) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `sku` (`sku`),
KEY `tiks` (`tiks`),
KEY `category` (`category`),
KEY `bests` (`best_seller`),
KEY `ne` (`is_new`),
KEY `qu` (`is_quick_shop`),
KEY `end_date` (`end_date`),
KEY `lat` (`latitute`),
KEY `lon` (`longitute`),
KEY `alias` (`alias`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `desc` (`description`),
FULLTEXT KEY `brand` (`brand`),
FULLTEXT KEY `advertiser` (`advertiser`),
FULLTEXT KEY `found_by` (`found_by`)
) ENGINE=MyISAM AUTO_INCREMENT=1861942 DEFAULT CHARSET=utf8
Assuming end_date is a date, convert the number into a date type using FROM_UNIXTIME()
SELECT *, 1 as distance
FROM deals
WHERE end_date > FROM_UNIXTIME(1381959736)
GROUP BY title
ORDER BY COALESCE(distance, 999999999), distance ASC
LIMIT 0, 3;
That way mysql doesn't have the cast every row's end_date to a string, which is very slow. It can also use an index if one exists, so make sure you have one:
create index deals_end_date on deals(end_date);