2017-06-03 22 views
0

我有這個表:MY-SQL表查詢給出不正確的結果,直到我優化表

CREATE TABLE `posts` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `user_id` int(11) NOT NULL, 
    `origin_post_id` int(11) NOT NULL, 
    `ref_post_id` int(11) NOT NULL, 
    `broker_user_id` int(11) NOT NULL, 
    `isshared` tinyint(4) NOT NULL, 
    `type` tinyint(4) NOT NULL, 
    `deal_type` tinyint(4) NOT NULL, 
    `title` varchar(200) NOT NULL, 
    `currency` varchar(10) NOT NULL, 
    `country_code` varchar(10) NOT NULL, 
    `price` float(10,2) NOT NULL, 
    `price_to` float(10,2) NOT NULL, 
    `sector` int(11) NOT NULL, 
    `protype` int(11) NOT NULL, 
    `sea_view` int(11) NOT NULL, 
    `sea_view_to` int(11) NOT NULL, 
    `area` int(11) NOT NULL, 
    `area_to` int(11) NOT NULL, 
    `area_mesure_type` tinyint(4) NOT NULL DEFAULT '1', 
    `building_area` int(11) NOT NULL, 
    `building_area_to` int(11) NOT NULL, 
    `building_area_mesure_type` tinyint(4) NOT NULL DEFAULT '1', 
    `bathrooms` int(11) NOT NULL, 
    `bathrooms_to` int(11) NOT NULL, 
    `rooms` int(11) NOT NULL, 
    `rooms_to` int(11) NOT NULL, 
    `location` varchar(300) NOT NULL, 
    `lat` float NOT NULL, 
    `lng` float NOT NULL, 
    `description` text NOT NULL, 
    `map_data` varchar(500) NOT NULL, 
    `shape_data` text NOT NULL, 
    `seo_title` varchar(300) NOT NULL, 
    `seo_keywords` varchar(300) NOT NULL, 
    `seo_description` varchar(300) NOT NULL, 
    `status` tinyint(4) NOT NULL, 
    `publish_date` date NOT NULL, 
    `expiry_date` date NOT NULL, 
    `request_status` enum('','NEW','ACCEPTED','REJECTED') NOT NULL, 
    `request_date` date NOT NULL, 
    `accept_date` date NOT NULL, 
    `share_per` float(5,2) NOT NULL, 
    `deal_completed` tinyint(4) NOT NULL, 
    `completed_by` int(11) NOT NULL, 
    `created` datetime NOT NULL, 
    `updated` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `type` (`type`), 
    KEY `deal_type` (`deal_type`), 
    KEY `price` (`price`), 
    KEY `sector` (`sector`), 
    KEY `protype` (`protype`), 
    KEY `sea_view` (`sea_view`), 
    KEY `area` (`area`), 
    KEY `building_area` (`building_area`), 
    KEY `bathrooms` (`bathrooms`), 
    KEY `rooms` (`rooms`), 
    KEY `lat` (`lat`), 
    KEY `lng` (`lng`), 
    KEY `status` (`status`), 
    KEY `price_to` (`price_to`), 
    KEY `sea_view_to` (`sea_view_to`), 
    KEY `area_to` (`area_to`), 
    KEY `building_area_to` (`building_area_to`), 
    KEY `bathrooms_to` (`bathrooms_to`), 
    KEY `rooms_to` (`rooms_to`), 
    KEY `request_status` (`request_status`), 
    KEY `request_date` (`request_date`), 
    KEY `accept_date` (`accept_date`), 
    KEY `ref_post_id` (`ref_post_id`), 
    KEY `deal_completed` (`deal_completed`), 
    KEY `location` (`location`(255)), 
    KEY `user_id` (`user_id`), 
    KEY `country_code` (`country_code`), 
    KEY `expiry_date` (`expiry_date`), 
    KEY `origin_post_id` (`origin_post_id`), 
    KEY `completed_by` (`completed_by`), 
    FULLTEXT KEY `title_location_description` (`title`,`location`,`description`), 
    CONSTRAINT `posts_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION 
) ENGINE=InnoDB AUTO_INCREMENT=833 DEFAULT CHARSET=utf8 

當我運行select count(id) from posts where deal_completed='1' and user_id='<anyuserid>'然後它給了零個記錄總是但是當我優化這種表然後它給正確的結果和一段時間後,我需要再次優化表格。

+0

你能發佈一個可重現的問題的例子嗎? –

+1

優化表格不應該改變數據。更重要的是,沒有'group by'的聚合查詢應該總是返回一行。 *列*的值可能爲0,但記錄數始終爲1. –

+0

價格永遠不會是FLOAT。這就是DECIMAL爲什麼發明 – Strawberry

回答

0

可能是因爲您使用的鍵太多。僅當您要使用該列搜索表時才使用密鑰,或者只能使用該列快速操作的順序來使用密鑰。如果不需要,不要使用太多的鍵,因爲您一次又一次地優化了表。嘗試儘可能標準化表格,然後嘗試使用FOREIGN KEYS。正如@Tim所說,如果它是一個int嘗試int。

+0

的帖子。非常感謝您的回覆。所有的鍵都在搜索中使用,所以我無法刪除任何一個。 –