Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query very very slow. Help please!

Author  Topic 

fametown
Starting Member

4 Posts

Posted - 2006-12-19 : 13:17:38
Hi!

I'm a spanish webdeveloper that make the website www.fametown.com, with a bigger pictures galleries.

I have a big problem in my SQL query that show the pictures... it need more than 20 seconds to complete. I found that here: "(F.IdPersonaje = 233) OR (F.IdPersonaje = 234)" its the problem. If i only select one F.IdPersonaje the query it's very very fast. But if i add OR... OR... OR... it's very slow. Can i use any alternative???

The website of this query is: http://www.fametown.com/sp/fotos.aspx?carpeta=victoriaadams&grp=5&debug=true

THANKS A LOT !!!



SELECT F.Id, C.Nick, F.Cpe, F.IdColaborador, F.Vistas, F.Nombre, F.Ruta, F.Ancho, F.Alto, F.Peso, F.Fecha, F.Puntos, F.NumVotos, P.Nombre As NombrePersonaje FROM Fotos F, Usuarios C, Personajes P WHERE F.Cpe < 60 AND ((F.IdPersonaje = 233) OR (F.IdPersonaje = 234)) AND F.IdColaborador = C.Id AND F.IdPersonaje = P.Id AND F.Id IN (SELECT TOP 1000 F.Id FROM Fotos F, Usuarios C, Personajes P WHERE F.Cpe < 60 AND ((F.IdPersonaje = 233) OR (F.IdPersonaje = 234)) AND F.IdColaborador = C.Id AND F.IdPersonaje = P.Id AND F.Id NOT IN (SELECT TOP 0 F.Id FROM Fotos F, Usuarios C, Personajes P WHERE F.Cpe < 60 AND ((F.IdPersonaje = 233) OR (F.IdPersonaje = 234)) AND F.IdColaborador = C.Id AND F.IdPersonaje = P.Id ORDER BY F.Fecha DESC, F.Nombre DESC, F.Id DESC) ORDER BY F.Fecha DESC, F.Nombre DESC, F.Id DESC) ORDER BY F.Fecha DESC, F.Nombre DESC, F.Id DESC

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-19 : 13:28:59
Try this
SELECT TOP 1000	f.Id,
c.Nick,
f.Cpe,
f.IdColaborador,
f.Vistas,
f.Nombre,
f.Ruta,
f.Ancho,
f.Alto,
f.Peso,
f.Fecha,
f.Puntos,
f.NumVotos,
p.Nombre As NombrePersonaje
FROM Fotos AS f
INNER JOIN Usuarios AS c ON c.Id = f.IdColaborador
INNER JOIN Personajes AS p ON p.Id = f.IdPersonaje
WHERE f.Cpe < 60
AND f.IdPersonaje IN (233, 234)
ORDER BY f.Fecha DESC,
f.Nombre DESC,
f.Id DESC


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

fametown
Starting Member

4 Posts

Posted - 2006-12-19 : 16:10:05
This query run fast... but i need to do a script that can "paginate" the records. If i select 20 pictures in each page i need a query that take "1 to 20", "20 to 40", "40 to 60"....

Can you make this mod in your query. THANKS!!!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-19 : 23:39:22
http://weblogs.sqlteam.com/jeffs/archive/2004/03/22/1085.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-20 : 04:24:31
[code]declare @stage table
(
rowid smallint identity(0, 1),
id ...,
nick ...,
cpe ...,
idcolaborador ...,
vistas ...,
nombre ...,
ruta ...,
ancho ...,
alto ...,
peso ...,
fecha ...,
puntos ...,
numvotos ...,
nombrepersonaje ...
)

insert @stage
(
id,
nick,
cpe,
idcolaborador,
vistas,
nombre,
ruta,
ancho,
alto,
peso,
fecha,
puntos,
numvotos,
nombrepersonaje
)
SELECT TOP 1000 f.Id,
c.Nick,
f.Cpe,
f.IdColaborador,
f.Vistas,
f.Nombre,
f.Ruta,
f.Ancho,
f.Alto,
f.Peso,
f.Fecha,
f.Puntos,
f.NumVotos,
p.Nombre
FROM Fotos AS f
INNER JOIN Usuarios AS c ON c.Id = f.IdColaborador
INNER JOIN Personajes AS p ON p.Id = f.IdPersonaje
WHERE f.Cpe < 60
AND f.IdPersonaje IN (233, 234)
ORDER BY f.Fecha DESC,
f.Nombre DESC,
f.Id DESC

SELECT Id,
Nick,
Cpe,
IdColaborador,
Vistas,
Nombre,
Ruta,
Ancho,
Alto,
Peso,
Fecha,
Puntos,
NumVotos,
NombrePersonaje
FROM @Stage
WHERE RowID / 20 = <YourPageNumberHere>
ORDER BY RowID[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -