[英]Why does this SQL query kill my server?
When I run this query on SQL server from a Varnish cache server. 当我从Varnish缓存服务器在SQL Server上运行此查询时。
select count(distinct email) from aiki_users, ocal_files where userid =
(select upload_user from ocal_files where upload_user = userid limit 1)
With aiki_users
table having > 80000 rows 使用
aiki_users
表具有> 80000行
and the ocal_files
table having > 30000 rows. ocal_files
表具有> 30000行。
It kills the server. 它会杀死服务器。 It is called from a php script via ajax.
它是通过ajax从php脚本调用的。 I have to close the page that calls it.
我必须关闭调用它的页面。
I don't know what it returns, but I know that the thing I want to do should be created with group by and having, but I don't know how. 我不知道它会返回什么,但是我知道我想做的事情应该由group by and have创建,但是我不知道如何做。
The aiki_users table looks like this: aiki_users表如下所示:
CREATE TABLE IF NOT EXISTS `aiki_users` (
`userid` int(9) unsigned NOT NULL auto_increment,
`username` varchar(100) NOT NULL default '',
`full_name` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`sex` varchar(25) NOT NULL,
`job` varchar(255) NOT NULL,
`password` varchar(100) NOT NULL default '',
`oauth_passcode` varchar(255) NOT NULL,
`oauth_token` varchar(255) NOT NULL,
`oauth_token_secret` varchar(255) NOT NULL,
`usergroup` int(10) NOT NULL default '0',
`email` varchar(100) NOT NULL default '',
`avatar` varchar(255) NOT NULL,
`homepage` varchar(100) NOT NULL default '',
`first_ip` varchar(40) NOT NULL default '0',
`first_login` datetime NOT NULL,
`last_login` datetime NOT NULL,
`last_ip` varchar(40) NOT NULL,
`user_permissions` text NOT NULL,
`maillist` int(1) NOT NULL,
`logins_number` int(11) NOT NULL,
`randkey` varchar(255) NOT NULL,
`is_active` int(5) NOT NULL,
PRIMARY KEY (`userid`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
I don't have correct access to the server, I can't see the defnition of ocal_files but: 我没有正确访问服务器的权限,看不到ocal_files的定义,但:
ocal_files.aiki_users = aiki_users.userid
Can anybody tell me how bad the query is and what it returns? 谁能告诉我查询有多糟糕以及查询返回什么?
您可以从主查询中的aiki_user和ocal_files中进行选择,而无需指定join子句,嵌套查询中的where不会停止全表连接...
I'd recommend running EXPLAIN PLAN on both queries and seeing what the database tells you. 我建议对两个查询都运行EXPLAIN PLAN,并查看数据库告诉您的内容。
My guess is that you have to do a TABLE SCAN, so the performance is terrible. 我的猜测是您必须执行TABLE SCAN,所以性能很糟糕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.