Need Help of Professional to Speed Up Query

Discussion in 'MySQL' started by deriklogov, Sep 7, 2007.

  1. #1
    I have an part of code
    $nr = mysql_num_rows(mysql_query("Select * from aranan where keyword = '$kelime'"));
    if($nr==0) {
    @mysql_query("Insert Into aranan (keyword,hit) values ('$kelime','1')");
    }else{
    @mysql_query("Update aranan set hit = hit+1 where keyword = '$kelime'");

    and that part killing server resources, is there any way to optimize it to speed up process ?
     
    deriklogov, Sep 7, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i'm not sure if this would work for you cause i don't know the language you're using.. but if you can do a try catch statement it could at least rid you of one search..

    try {
    @mysql_query("Update aranan set hit = hit+1 where keyword = '$kelime'");
    }
    catch {
    @mysql_query("Insert Into aranan (keyword,hit) values ('$kelime','1')");
    }

    so if $kelime is in the table that is updated, if not it inserts the new row
     
    Jamie18, Sep 7, 2007 IP
  3. symmetric

    symmetric Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you have an index on the keyword column?

    If the aranan table is large and you don't have an index, then a table scan would be costly.
     
    symmetric, Sep 7, 2007 IP
  4. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Language is php
     
    deriklogov, Sep 7, 2007 IP
  5. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #5
    I dont have index , and table is over 200,000 , would the index help me ? how much faster it could be with index ?
     
    deriklogov, Sep 7, 2007 IP
  6. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Is there any way to check how fast is the query executed ?
     
    deriklogov, Sep 7, 2007 IP
  7. symmetric

    symmetric Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Most of my work is with MS SQL Server, not MySQL. That said, the concepts are similar.

    In my work, I've seen many cases where the addition of an index leads to a performance increase of 10x, 50x or even 100x.

    Furthermore, experience suggests that one should test rather than assume. I suggest that you just add the index to see how it performs.
     
    symmetric, Sep 7, 2007 IP
  8. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #8
    Thank you
    help a lot
     
    deriklogov, Sep 7, 2007 IP
  9. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #9
    deriklogov - You will notice a remarkable increase in speed once you
    create an index in the table arnan on "Keyword".

    To see the time taken to execute the query, if you run the query
    in phpmyadmin, you can see the time.
     
    Kuldeep1952, Sep 8, 2007 IP
  10. fabriciogr

    fabriciogr Active Member

    Messages:
    958
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    73
    #10
    like everyone is commenting... in mysql indexes make a huge difference as the whole concept is that the position of each value indexed is stored in another table and it can now access it directly... and not have to read row by row until it finds the one you're looking for...

    also another bit you might like to use which is more of a way to reduce lines of your code is:

    REPLACE INTO test VALUES ('agent2', number+1);

    the replace function does exactly what u're trying to do with doing a lookup first and if it doesn't exists inserting else updating...

    i think it looks nicer... cooler coding... :D :D
     
    fabriciogr, Sep 9, 2007 IP