• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Best way to load data from database (MySQL r41)
#1
Rainbow 
What is the most efficient way to load data from the database.
For example, how could I make the function below more efficient.

Code:
function loadDealerShip( ) {
    new DS_Models = cache_num_rows( ), x, result;
    if( DS_Models == 0 ) return print( "Loaded models: no rows detected" );
    for( new i, j = DS_Models; i != j;  ) {
        cache_get_value_name_int( i, "ID", result ), x = result, dsVariable[ x ][ dsID ] = x;
        cache_get_value_name_int( i, "Model", dsVariable[ x ][ dsModel ] );
        cache_get_value_name_int( i, "Stock", dsVariable[ x ][ dsStock ] );
        cache_get_value_name_int( i, "Speed", dsVariable[ x ][ dsSpeed ] );
        cache_get_value_name_int( i, "Price", dsVariable[ x ][ dsPrice ] );
        cache_get_value_name_int( i, "Type", dsVariable[ x ][ dsType ] );
        cache_get_value_name( i, "Car", dsVariable[ x ][ dsName ] );
    } printf( "Loaded models: %d", DS_Models );
return true; }


The 'ID'?starts at 1 and reaches over 100.
  Reply
#2
Why do you have x and result?
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#3
Good question.. Is there something else I can do to improve it?



Code:
function loadDealerShip( ) {

? ? new DS_Models = cache_num_rows( );

? ? if( DS_Models == 0 ) return print( "Loaded models: no rows detected" );

? ? for( new i, j = DS_Models; i != j;  ) {

? ? ? ? cache_get_value_name_int( i, "ID", dsVariable[ i ][ dsID ] );

? ? ? ? cache_get_value_name_int( i, "Model", dsVariable[ i ][ dsModel ] );

? ? ? ? cache_get_value_name_int( i, "Stock", dsVariable[ i ][ dsStock ] );

? ? ? ? cache_get_value_name_int( i, "Speed", dsVariable[ i ][ dsSpeed ] );

? ? ? ? cache_get_value_name_int( i, "Price", dsVariable[ i ][ dsPrice ] );

? ? ? ? cache_get_value_name_int( i, "Type", dsVariable[ i ][ dsType ] );

? ? ? ? cache_get_value_name( i, "Car", dsVariable[ i ][ dsName ] );

? ? } printf( "Loaded models: %d", DS_Models );

return true; }
  Reply
#4
Probably not, either way you shouldn't worry to much about 1-time reads, worry about the peak usage queries instead (like writing and reading user data).
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#5
For most intents and purposes that is probably fine. High frequency database operations is what you should worry about as stated by Pinch, and in those cases the best way to optimize is by decreasing frequency (caching).
  Reply
#6
It is more efficient to use this method for most rescues

Code:
//global

new gQuery[ 256 ];



//local

gQuery[ 0 ] = EOS;

mysql_format( SQL, gQuery, sizeof gQuery, "UPDATE `players` SET `Hours`='%0.3f' WHERE `ID`='%d';", playerVariable[ playerid ][ pHours ], playerVariable[ playerid ][ pSQL ] );

mysql_tquery( SQL, gQuery, "", "" );



compared to this?

Code:
//local

new Query[ 70 ];

mysql_format( SQL, Query, sizeof Query, "UPDATE `players` SET `Hours`='%0.3f' WHERE `ID`='%d';", playerVariable[ playerid ][ pHours ], playerVariable[ playerid ][ pSQL ] );

mysql_tquery( SQL, Query, "", "" );
  Reply
#7
Local, always
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply


Forum Jump: