我正在用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 ;
答案 0 :(得分:0)
你喜欢的声明似乎没有使用通配符,我认为没有理由在这里使用它们。尝试将它们改为等号。为表格编制索引也可以为您提供帮助。
答案 1 :(得分:0)
您似乎没有外键关系的索引。
尝试:
create index who on votes (who);
create index q1 on users (picture1, cp1, gender);
此外,您似乎对“喜欢”和“=”感到困惑 - 在比较整数列(例如CP1)时,请使用cp1 < 1
或who = 12707264
。
答案 2 :(得分:0)
首先,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
,并且应该在列中没有图片存储NULL