在我优化表之前,MY-SQL表查询给出了错误的结果

时间:2017-06-03 11:49:16

标签: mysql

我有这张桌子:

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>'时,它总是给出零记录,但是当我优化该表时,它会给出正确的结果,并且在一段时间之后我需要再次优化该表。

1 个答案:

答案 0 :(得分:0)

可能是因为您使用的钥匙太多了。只有在您要使用该列搜索表或按该列进行排序时才应使用密钥,以便快速操作。如果不需要,请不要使用太多的键,因为您一次又一次地优化了表。尝试尽可能地规范化表格并尝试使用FOREIGN KEYS。正如@Tim所说,如果是int try int。