Jump to content

Anyway to make this more efficient?


Controlled

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...