1

我正在用 PHP 编写一个 webapp,可以对用户列表进行投票。我用来从表中拉出用户的查询非常慢。我怀疑有一种更有效的方法来检查目标用户是否已经被活动用户投票。

SELECT * 
FROM users
WHERE id NOT IN (
    SELECT ratedid
    FROM votes
    WHERE who LIKE 12707264
)
AND picture1 NOT LIKE  ''
AND cp1 <  '10'
AND gender NOT LIKE  'male'
ORDER BY RAND( ) 
LIMIT 1

表数据如下:

>SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
>SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `notchus_userdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` bigint(20) NOT NULL,
  `username` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `bio` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `picture1` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `cp1` int(100) DEFAULT NULL,
  `picture2` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `picture3` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `friends` blob,
  `relationship_status` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `relationship_interest` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `verified` int(1) NOT NULL,
  `gender` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `birthday` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `hometown` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `citylocation` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `oauth_provider` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `oauth_uid` int(11) NOT NULL,
  `ratchet` int(1) DEFAULT NULL,
  `boss` int(1) DEFAULT NULL,
  `isadmin` int(1) DEFAULT NULL,
  `views` int(10) NOT NULL,
  `reviews` int(10) NOT NULL,
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `isuser` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


Server version: 5.1.70-cll
-- PHP Version: 5.3.17

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `notchus_userdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `votes`
--

CREATE TABLE IF NOT EXISTS `votes` (
  `uqid` int(11) NOT NULL AUTO_INCREMENT,
  `value` tinyint(4) NOT NULL,
  `picture` int(11) DEFAULT NULL,
  `ratedid` bigint(20) DEFAULT NULL,
  `comment` int(11) DEFAULT NULL,
  `quote` int(11) DEFAULT NULL,
  `who` bigint(20) NOT NULL,
  `votedate` int(11) NOT NULL,
  `control` varchar(100) NOT NULL,
  PRIMARY KEY (`uqid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1084 ;
4

3 回答 3

0

首先,LIKE 仅用于进行通配符匹配。我很惊讶您在对整数执行 LIKE 时没有收到错误。再说一次,MySQL 所做的一切都没有让我感到惊讶。

SELECT * 
FROM users
WHERE id NOT IN (
    SELECT ratedid
    FROM votes
    WHERE who = 12707264
)
AND picture1 <> ''
AND cp1 <  '10'
AND gender <>  'male'
ORDER BY RAND( ) 
LIMIT 1

另请注意,如果picture1可以作为空字符串,那么您不应该NOT NULL在列上有 a,并且您应该在没有图片的列中存储 NULL。

于 2013-10-08T17:07:32.923 回答
0

您的like 语句似乎没有使用通配符,我认为没有理由在这里使用它们。尝试将它们更改为等号。索引你的表也可以帮助你。

于 2013-10-08T16:52:03.417 回答
0

您似乎没有关于外键关系的索引。

尝试:

create index who on votes (who);

create index q1 on users (picture1, cp1, gender);

此外,您似乎对“like”和“=”感到困惑 - 在比较整数列(例如 CP1)时,使用cp1 < 1or who = 12707264

于 2013-10-08T16:58:41.833 回答