Similar Threads:
1.Need to optimize a long query with JOIN
Hello,
I wanted to know if there's anyone that could help me with this
problem. My query takes too much time to execute and I don't know how
to make it better.
SELECT SQL_CALC_FOUND_ROWS i.*, DATE_FORMAT(i.date, '%d-%m-%y') as
date, u.login
FROM img i
LEFT JOIN comments c ON c.imgid=i.id
LEFT JOIN users u ON u.id=i.userid
LEFT JOIN flags f ON i.id=f.imgid
WHERE i.deleted=0
AND c.date > f.lastview
AND c.userid!=530
AND f.userid=530
GROUP BY i.id ORDER BY i.id DESC LIMIT 0,12
The problem is that this query needs to examinate more that 1M rows
(by running EXPLAIN). There's 150k rows on table "comments", 4k on
users, 35k on img and 1,8 M on "flags". All table have indexes and PK.
Thanks a lot,
Jean-Baptiste
2.Need help ordering by the count of a join table in x number of hours
I'm a bit stumped on this problem (definitely not a SQL guru). I have
2 tables like so:
create table items (
id int unsigned not null auto_increment,
user_id int unsigned not null,
title varchar(64) not null,
link varchar(128) not null,
created_on timestamp,
primary key (id)
) engine=innodb default charset=utf8;
create table hits (
id int unsigned not null auto_increment,
remote_ip varchar not null,
item_id int unsigned not null,
created_at datetime not null,
primary key (id)
) engine=innodb default charset=utf8;
How can I find all items ordered by the count of hits occuring within
x number of hours? So, basically I don't want a where clause. I want
to retrieve *all* items. I just want them to be weighted by the
number of hits they've received in the last hour or 6 hours or
whatever...
Thanks in advance
3.I need help optimizing this query
Here is the query I am currently using.
It seems to take forever. It keeps wanting to use filesort and a temporary can
anyone plz help me. I will send the creates for both tables.
select collections.`id`,collections.`subject`,collections.`numfiles`,collections.
`groups`,
collections.`size`,collections.`from`,collections.`date`,collections.`nfo`
FROM temp.collections,temp.nfo where match (nfo.nfo) against ('$searchterm'
in boolean mode) and nfo.cid = collections.id order by `date` desc limit 0,50
CREATE TABLE `nfo` (
`id` mediumint(6) unsigned NOT NULL auto_increment,
`cid` int(10) unsigned NOT NULL,
`fid` int(10) unsigned NOT NULL,
`nfo` mediumtext NOT NULL,
`filename` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
KEY `cid` (`cid`),
KEY `fid` (`fid`),
FULLTEXT KEY `nfo` (`nfo`)
) ENGINE=MyISAM AUTO_INCREMENT=152507 DEFAULT
CHARSET=latin1
CREATE TABLE `collections` (
`id` int(10) unsigned NOT NULL auto_increment,
`subject` text NOT NULL,
`filename` text NOT NULL,
`numfiles` smallint(4) unsigned NOT NULL default '0',
`groups` text NOT NULL,
`partids` mediumtext NOT NULL,
`size` bigint(20) unsigned NOT NULL default '0',
`from` tinytext NOT NULL,
`date` int(10) unsigned NOT NULL default '0',
`group1` smallint(4) unsigned NOT NULL,
`group2` smallint(4) unsigned default NULL,
`group3` smallint(4) unsigned default NULL,
`group4` smallint(4) unsigned default NULL,
`group5` smallint(4) unsigned default NULL,
`nfo` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `filename_U` (`filename`(255)),
KEY `from` (`from`(50)),
KEY `date` (`date`),
KEY `Size` (`size`),
KEY `nfo` (`nfo`),
KEY `groupindex` USING BTREE (`group1`,`group2`,`group3`,`group4`,
`group5`),
FULLTEXT KEY `Subject_full` (`subject`)
) ENGINE=MyISAM AUTO_INCREMENT=1865103 DEFAULT
CHARSET=latin1 MIN_ROWS=100000 MAX_ROWS=2500000
AVG_ROW_LENGTH=500
4.HELP WITH QUERY - I THINK I need a JOIN, not sure
If anyone could help with this, I'd be much obliged.
Basically I have one table, users, with these columns:
user_id
name
email
and a 2nd table called user_interests with fields:
interest_id
user_id
interest_text
I want to run one query that gives me all the users in the "users"
table and adds columns that contain the "interest_text" of any matching
rows in the "user_interests" table
i.e.
user_id: 10
name: andy
email: XXXX@XXXXX.COM
interest1: soccer
interest2: music
interest3: movies
This seems fairly simple. What do I need to do to make this happen?
Thanks!
5.Need help optimizing a batch query
I have a sproc that will return several tables, and it looks something like
this:
SELECT * FROM TABLEA WHERE somelongasswhereclause
SELECT * FROM TABLEB WHERE TableBID IN (SELECT foreignKeyToTableB FROM
TABLEA WHERE somelongasswhereclause)
SELECT * FROM TABLEC WHERE TableBID IN (SELECT foreignKeyToTableC FROM
TABLEA WHERE somelongasswhereclause)
So what I'm trying to do here is get a buncha records from TableA, and then
get a buncha records from TableB that relate to TableA and so on. I could
just do a JOIN, but I really would like the data to be in separate tables.
My problem is my somelongasswhereclause. Its a pretty long WHERE clause
with lots of conditions, and it seems to me that having to evaluate them
over and over is a waste. What options do I have here? I was thinking of
opening a cursor on TABLEA and creating an IN clause of the TABLEA IDs, so
that in the following queries I would never have to say "...FROM TABLEA
WHERE somelongasswhereclause" again. I could just say "...FROM TABLEA WHERE
tableAID IN (...)", but I still need to return TABLEA and how would I do
that if I made a cursor out of it?
6. Need Help Optimizing A Query
7. Need help optimizing this query
8. Need help to optimize query