SQL Limit Function

Discussion in 'Mac OS X Server, Xserve, and Networking' started by macuser154, Dec 12, 2009.

  1. macuser154 macrumors 6502

    Joined:
    Jan 17, 2009
    Location:
    UK
    #1
    I don't really know where this would go. But does anyone know how I can use the limit parameter in SQL to only bring back 25% of the total amount of records?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I'd probably just create a temp variable that calculates it
    Code:
    SET @quarter = (SELECT COUNT(*) FROM mytable) / 4;
    SELECT * FROM mytable LIMIT ROUND(@quarter);
    Haven't tested this though.
     
  3. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #3
    I wouldn't recommend that on a very large table. Talk about a resource hog with two full table scans.

    I'd recommend limiting it to a distinct number. Using percentages, if the table has 100 rows, then you pull back 25. If it has 1,000,000, then you're pulling back 250,000, which is most likely not what the user wanted.
     
  4. macuser154 thread starter macrumors 6502

    Joined:
    Jan 17, 2009
    Location:
    UK
    #4
    This limiter would be used on a View, which tends not to have many records in it. Would that help keep the resource usage down?
     
  5. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #5
    A view is transparent and holds no actual data, so no it won't help. How many rows does this view contain?
     

Share This Page