Coldfusion Many Loops vs. Query Request
source link: https://www.codesd.com/item/coldfusion-many-loops-vs-query-request.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Coldfusion Many Loops vs. Query Request
I am deleting login records in my database that don't have a corresponding logout record, but right now it's very slow It does this:
First it gets the queries to loop over to check to delete Next it needs to find out if the next record for that user is a login or logout, if it's a login, I delete it.
To get the next record of that type it does this query of query:
<cfquery dbtype="query" name="getnext" maxrows="1">
SELECT * FROM getlogs WHERE id > #id# AND logType = 'login'
</cfquery>
But it's slow, doing it thousands of times makes it take about 56 seconds.
What would be a faster way to do this? Would another cfloop inside my loop (basicly a loop until I get to the row I want) be faster? Is there another way?
This sounds like something that can be done entirely in one query -- perhaps something like this:
delete from login_table t
where exists (
select id
from login_table
where id > t.id
and logtype = 'login'
)
This has nothing to do with ColdFusion per se; the same approach would apply in any environment. If this is a maintenance function that has no synchronous dependence on your application, you could even stick it into a stored procedure invoked automatically by a recurring "cleanup" task in the database itself.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK