Am i doing the right thing?

Discussion in 'PHP' started by slaydragon, Jun 12, 2008.

  1. #1
    hi,

    i am asking some php and mysql questions here. I have a table called, articles. And in that articles table, i have 10000 rows, increasing day by day.

    I am displaying 10 of those article in one php page with pagination. (10 per age) (something like search engine). Currently, i am using php select * to display all articles and them i apply pagination to them. That mean's whenever i load the page, sql queries are run to display all the 10000 rows.

    Currently, the php page still load fast. But i am wondering, if my database gets bigger, will it cause serverload and lagging time when lagging the page? Cos i am running a sql query to display all the results. Do you people use this method when displaying data? Am i doing the right thing?
     
    slaydragon, Jun 12, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    you are doing wrong thing

    Just fetch only records which u want to show on page not all 10000

    Select * from articles Limit 0,99 <--- first page

    Select * from articles Limit 100,199 <--- first page

    Select * from articles Limit 200,299 <--- first page

    Just need a for loop

    I hopy u get the basic point

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  3. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi, but i need to display all the 10000 rows.., so that will be the problem on my pagination coding? but in order to count the number of pages and seperate them into different pages, first i need to run a select * to count the number of rows in the database b4 i can continue doing the sql limi
     
    slaydragon, Jun 12, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    If you want to do a high/low limit you can use this:

    
    
    $page = (int)$_GET['page']; //page 1, 2, 3, 4 ....
    $result_num = 10; //number of rows you want displayed
    
    $low_limit = $page*$result_num;
    $high_limit = (($page+1)*$result_num)-1;
    
    $query = "Select * from articles Limit $low_limit,$high_limit";
    
    PHP:
     
    jestep, Jun 12, 2008 IP
  5. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #5
    yes u will need to count

    counting does not take time

    Just devide total rows number by 10

    and u will get number of pages

    Jastep gave the actual code and i just want that you write the code yourself

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  6. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hi, if i use select * FROM articles and i count it.. will it takes load? like say i have a million articles.. the dividing and getting the pages i undertstand.. just wondering if it takes load.. cos that means everytime a person load a page, i have to count all the rows first.. what happens if i had one million articles?
     
    slaydragon, Jun 12, 2008 IP
  7. clobberx

    clobberx Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    Database Indexing

    the best you index, the fast you get the data
     
    clobberx, Jun 12, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Limit was implement in sql for paging. If you have 1 million record to select. You can use LIMIT to break apart your result into pieces.

    For example:You have 1 million records.

    SELECT * FROM articles LIMIT 0,20;

    This query doesn't select all your 1 million record, its only select a result row 0 to 20 which is take a second to load.
     
    php-lover, Jun 12, 2008 IP
    slaydragon likes this.
  9. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #9
    hi, but inorder to determine how many pages i have, i need to count all the rows, divided by number of rows per page.. so in the end i still need to count all the rows..
     
    slaydragon, Jun 12, 2008 IP
  10. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello,

    To count the rows you can use:

    SELECT COUNT(*) FROM articles;

    Which will return the number of rows in the table.

    Sky22
     
    sky22, Jun 12, 2008 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    In generall, querying by using Select * is much slower than selecting by specific column.

    Ideally you should select only the columns that you are need in your results.

    IE(Select count(id))

    or:

    Select id, name, url
     
    jestep, Jun 12, 2008 IP
  12. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #12
    will using this statement alot of serverload if i have 1 million rows?
     
    slaydragon, Jun 12, 2008 IP
  13. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #13
    Yes you right, you need to count all the rows.

    If you test count your rows in mysql command line,

    its takes 0.00 sec ;)
     
    php-lover, Jun 12, 2008 IP