dongyan7172 2012-09-20 04:55
浏览 39
已采纳

SQL select查询需要很长时间

I am using the below SQL query but it takes more than 180 sec to execute. Is there a way to speed it up ? This SQL give me the pic_id and of all the females.

SELECT pic_id, small
FROM picture
WHERE hide =0
AND userhide =0
AND  `fbid` 
IN (

      SELECT fbid
      FROM user
      WHERE gender =  "female"
   )

ORDER BY  `picture`.`pic_id` ASC 
LIMIT 1500 , 200

The Explain SQL

id select_type         table    type   possible_keys  key      key_len  ref   rows  Extra
1  PRIMARY             picture  index  NULL           PRIMARY  4        NULL  1700  Using where
2  DEPENDENT SUBQUERY  user     ALL    NULL           NULL     NULL     NULL  7496  Using where

--- Result of explain statement for Tim's sql answer --

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  picture ALL NULL    NULL    NULL    NULL    41443   Using where; Using temporary; Using filesort
1   SIMPLE  user    ALL NULL    NULL    NULL    NULL    7501    Using where; Using join buffer

-- Structure ---

CREATE TABLE `user` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`first_name` varchar(255) CHARACTER SET utf8 NOT NULL,
`username` varchar(255) CHARACTER SET utf8 NOT NULL,
`birthday` date NOT NULL,
`location` varchar(255) CHARACTER SET utf8 NOT NULL,
`gender` varchar(6) CHARACTER SET utf8 NOT NULL,
`created` date NOT NULL,
`fbid` bigint(50) NOT NULL,
`token` varchar(255) CHARACTER SET utf8 NOT NULL,
`relationship_status` varchar(20) CHARACTER SET utf8 NOT NULL,
`smallest` varchar(255) CHARACTER SET utf8 NOT NULL,
`email` varchar(40) CHARACTER SET utf8 NOT NULL,
`ref` varchar(15) NOT NULL,
PRIMARY KEY (`sid`),
KEY `gender` (`gender`),
KEY `fbid` (`fbid`)
) ENGINE=MyISAM AUTO_INCREMENT=7595 DEFAULT CHARSET=latin

---- structure of picture table ---

CREATE TABLE `picture` (
 `fbid` bigint(50) NOT NULL,
 `pic_id` int(11) NOT NULL AUTO_INCREMENT,
 `pic_location` varchar(255) NOT NULL,
 `hide` int(1) NOT NULL,
 `small` varchar(255) NOT NULL,
 `userhide` int(1) NOT NULL,
 `likes` int(10) NOT NULL,
 `hot` int(1) NOT NULL,
 PRIMARY KEY (`pic_id`),
 UNIQUE KEY `pic_location` (`pic_location`),
 UNIQUE KEY `small` (`small`),
 KEY `fbid` (`fbid`),
 KEY `hide` (`hide`),
 KEY `userhide` (`userhide`)
) ENGINE=MyISAM AUTO_INCREMENT=42749 DEFAULT CHARSET=latin1
  • 写回答

2条回答 默认 最新

  • duanre1891 2012-09-20 04:59
    关注

    try something like this:

    SELECT pic_id, small
    FROM picture
    INNER JOIN user ON ( picture.fbid = user.fbid and user.gender='female' )
    WHERE hide =0
    AND userhide =0
    ORDER BY  `picture`.`pic_id` ASC 
    LIMIT 1500 , 200
    

    I put gender in the join because a query will not return rows that don't have a match on an inner join.

    You should also read this stack overflow topic

    EDIT:

    make sure you have indexed the following fields:

    picture.fbid
    user.fbid
    user.gender
    picture.hide
    picture.userhide
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改