Controlled Posted April 22, 2014 Share Posted April 22, 2014 I have this script to delete Houses, Cars, and Rented Houses that are owned by someone that is inactive. Is their anyway to make it not completely lag out the server? And make it a bit slower but allow people to still type and such? -- Inactive houses - 1 month local mQuery1 = mysql:query("select interiors.id from interiors left join characters on characters.`id` = interiors.`owner` where (characters.lastlogin < ( NOW() - interval 28 day))") local count1 = 0 if (mQuery1) then while true do local row = mysql:fetch_assoc(mQuery1) if not row then break end local interiorID2 = (row["id"]) local interiorID = tonumber(row["id"]) exports['house-system']:removeHouse(thePlayer, interiorID, true, true) end end --Impounded vehicles - 2 weeks local mQuery2 = mysql:query("select vehicles.id, vehicles.model from vehicles where vehicles.Impounded != 0 and vehicles.Impounded < (".. getRealTime().yearday .."- 28)") if (mQuery2) then while true do local row = mysql:fetch_assoc(mQuery2) if not row then break end local vehicleID = tonumber(row["id"]) local vehiclemodel = tonumber(row["model"]) local vehicleID2 = (row["id"]) mysql:query_free("DELETE FROM `vehicles` WHERE `id`='".. vehicleID .."'") -- Delete the vehicle mysql:query_free("DELETE FROM `items` WHERE `type`='2' AND `owner`='".. vehicleID .."'") -- Delete its contents end end --Rented Cars local mQuery4 = mysql:query("select vehicles.id, vehicles.model from vehicles where vehicles.Impounded != 0 and rent = 1") if (mQuery4) then while true do local row = mysql:fetch_assoc(mQuery4) if not row then break end local vehicleID = tonumber(row["id"]) local vehicleID2 = (row["id"]) local vehiclemodel = tonumber(row["model"]) mysql:query_free("DELETE FROM `vehicles` WHERE `id`='".. vehicleID .."'") -- Delete the vehicle mysql:query_free("DELETE FROM `items` WHERE `type`='2' AND `owner`='".. vehicleID .."'") -- Delete its contents end end local mQuery3 = mysql:query("select vehicles.id from vehicles left join characters on characters.`id` = vehicles.`owner` where (characters.lastlogin < ( NOW() - interval 45 day))") if (mQuery3) then while true do local row = mysql:fetch_assoc(mQuery3) local vehicleID = tonumber(row["id"]) mysql:query_free("DELETE FROM `vehicles` WHERE `id`='".. vehicleID .."'") -- Delete the vehicle mysql:query_free("DELETE FROM `items` WHERE `type`='2' AND `owner`='".. vehicleID .."'") -- Delete its contents end Link to comment
Moderators IIYAMA Posted April 22, 2014 Moderators Share Posted April 22, 2014 The "while" loops are very laggy when you don't use them careful. The loop at line 48 will never be broken? Link to comment
tosfera Posted April 22, 2014 Share Posted April 22, 2014 There sure is an easier way to do so, here is an example of the impounded vehicles; local impoundedVehicles = mysql:query ( "SELECT `vehicles`.`id`, `vehicles`.`model` FROM `vehicles` WHERE `vehicles`.`Impounded` != 0 AND `vehicles`.`Impounded` < ( ".. getRealTime().yearday .." - 28 ) " ); if ( impoundedVehicles ) then while ( mysql:fetch_assoc ( impoundedVehicles ) ) do local row = mysql:fetch_assoc ( impoundedVehicles ); mysql:query_free ( "DELETE FROM `vehicles` WHERE `id` = '".. tonumber ( row["id"] ) .."' " ); mysql:query_free ( "DELETE FROM `items` WHERE `type` = 2 AND `owner` = '".. tonumber ( row [ "id" ] ) .."' " ); end end I've removed the following things and added a comment why; while true do -- playing with a true, breaking when you have no row. Can be replaces with the mysql:fetch_assoc ( if I'm right ) local vehicleID = tonumber(row["id"]) -- I removed this, added it into the query itself. Not sure if it works, you should try. local vehiclemodel = tonumber(row["model"]) -- this isn't getting used. local vehicleID2 = (row["id"]) -- this isn't getting used either Also, you should use mysql_real_escape to ensure that your database can't be hacked with simple injections. Link to comment
Controlled Posted April 22, 2014 Author Share Posted April 22, 2014 Thanks! I will try everything you said and see how this works. Link to comment
tosfera Posted April 22, 2014 Share Posted April 22, 2014 Another thing that's being used in PHP is to initalize the variable in the condition of your while loop, not sure if it works in Lua but you can give it a shot; local impoundedVehicles = mysql:query ( "SELECT `vehicles`.`id`, `vehicles`.`model` FROM `vehicles` WHERE `vehicles`.`Impounded` != 0 AND `vehicles`.`Impounded` < ( ".. getRealTime().yearday .." - 28 ) " ); if ( impoundedVehicles ) then while ( local row = mysql:fetch_assoc ( impoundedVehicles ) ) do mysql:query_free ( "DELETE FROM `vehicles` WHERE `id` = '".. tonumber ( row["id"] ) .."' " ); mysql:query_free ( "DELETE FROM `items` WHERE `type` = 2 AND `owner` = '".. tonumber ( row [ "id" ] ) .."' " ); end end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now