2017-06-03 49 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

的帖子。非常感谢您的回复。所有的键都在搜索中使用,所以我无法删除任何一个。 –