#272 ✓invalid
mephux

per_page and composite_primary_keys

Reported by mephux | June 25th, 2009 @ 03:42 AM

It seems per_page breaks when using the composite_primary_keys plugin. Seems to put a limit for both ids and looping through both. So :per_page => 5 would turn into 10 items per page, 2 of each.

Comments and changes to this ticket

  • mephux

    mephux June 25th, 2009 @ 09:59 AM

    Ok, I have been working on this all night and can't seem to think of an easy fix. If you need any type of data please let me know.

  • Mislav

    Mislav June 25th, 2009 @ 05:43 PM

    • State changed from “new” to “open”

    You'll have to put this into SQL terms for me. What does the plugin do? What kind of SQL gets generated? Where is it wrong? How it should be correct?

    After supplying me with this info I will know how to make will_paginate handle this. Thanks

  • mephux

    mephux June 25th, 2009 @ 10:37 PM

    The plugin basically just adds composite PK support as transparently as possible.

    As for the SQL syntax:

    Code:

    @events ||= Event.all.paginate(:page => params[:page], :per_page => 5, :order => 'timestamp DESC')

    Output:

    Processing EventsController#index (for 127.0.0.1 at 2009-06-25 03:53:56) [GET]
    Event Load (0.3ms) SELECT * FROM event ORDER BY timestamp DESC LIMIT 0, 5 Event Columns (1.8ms) SHOW FIELDS FROM event SQL (0.7ms) SELECT count(*) AS count_all FROM event Rendering template within layouts/application
    Rendering events/index
    Signature Columns (1.3ms) SHOW FIELDS FROM signature Signature Load (0.4ms) SELECT * FROM signature WHERE (signature.sig_id = 37) Iphdr Columns (7.7ms) SHOW FIELDS FROM iphdr Iphdr Load (2.2ms) SELECT * FROM iphdr WHERE ((iphdr.sid=7 AND iphdr.cid=471)) Sensor Columns (2.0ms) SHOW FIELDS FROM sensor Sensor Load (1.4ms) SELECT * FROM sensor WHERE (sensor.sid = 7) Signature Load (0.2ms) SELECT * FROM signature WHERE (signature.sig_id = 29) Iphdr Load (0.2ms) SELECT * FROM iphdr WHERE ((iphdr.sid=7 AND iphdr.cid=470)) CACHE (0.0ms) SELECT * FROM sensor WHERE (sensor.sid = 7) Signature Load (0.2ms) SELECT * FROM signature WHERE (signature.sig_id = 15) Iphdr Load (0.2ms) SELECT * FROM iphdr WHERE ((iphdr.sid=7 AND iphdr.cid=469)) CACHE (0.0ms) SELECT * FROM sensor WHERE (sensor.sid = 7) SigClass Columns (1.5ms) SHOW FIELDS FROM sig_class SigClass Load (1.1ms) SELECT * FROM sig_class WHERE (sig_class.sig_class_id = 2) CACHE (0.0ms) SELECT * FROM signature WHERE (signature.sig_id = 29) Iphdr Load (0.2ms) SELECT * FROM iphdr WHERE ((iphdr.sid=7 AND iphdr.cid=468)) CACHE (0.0ms) SELECT * FROM sensor WHERE (sensor.sid = 7) CACHE (0.0ms) SELECT * FROM signature WHERE (signature.sig_id = 15) Iphdr Load (0.2ms) SELECT * FROM iphdr WHERE ((iphdr.sid=7 AND iphdr.cid=467)) CACHE (0.0ms) SELECT * FROM sensor WHERE (sensor.sid = 7) CACHE (0.0ms) SELECT * FROM sig_class WHERE (sig_class.sig_class_id = 2) Rendered events/event (36.2ms)
    Rendered events/
    event (3.7ms)
    Rendered events/event (2.9ms)
    Rendered events/
    event (2.8ms)
    Rendered events/event (2.8ms)
    Rendered layouts/
    header (0.2ms)
    Rendered layouts/_footer (0.1ms)
    Completed in 57ms (View: 33, DB: 22) | 200 OK [http://0.0.0.0/]

    'Where is it wrong? How it should be correct?'

    Well, it seems to loop the data 5 times or whatever is set for :per_page. It should just show 5 events per page.

    If this is not helpful please let me know. I would really like to help to fix this issue.

  • mephux

    mephux June 25th, 2009 @ 10:41 PM

    Ok that looked horrible sorry.

    
    Processing EventsController#index (for 127.0.0.1 at 2009-06-25 03:53:56) [GET]
      Event Load (0.3ms)   SELECT * FROM `event` ORDER BY timestamp DESC LIMIT 0, 5
      Event Columns (1.8ms)   SHOW FIELDS FROM `event`
      SQL (0.7ms)   SELECT count(*) AS count_all FROM `event` 
    Rendering template within layouts/application
    Rendering events/index
      Signature Columns (1.3ms)   SHOW FIELDS FROM `signature`
      Signature Load (0.4ms)   SELECT * FROM `signature` WHERE (`signature`.`sig_id` = 37) 
      Iphdr Columns (7.7ms)   SHOW FIELDS FROM `iphdr`
      Iphdr Load (2.2ms)   SELECT * FROM `iphdr` WHERE ((`iphdr`.`sid`=7 AND `iphdr`.`cid`=471)) 
      Sensor Columns (2.0ms)   SHOW FIELDS FROM `sensor`
      Sensor Load (1.4ms)   SELECT * FROM `sensor` WHERE (`sensor`.`sid` = 7) 
      Signature Load (0.2ms)   SELECT * FROM `signature` WHERE (`signature`.`sig_id` = 29) 
      Iphdr Load (0.2ms)   SELECT * FROM `iphdr` WHERE ((`iphdr`.`sid`=7 AND `iphdr`.`cid`=470)) 
      CACHE (0.0ms)   SELECT * FROM `sensor` WHERE (`sensor`.`sid` = 7) 
      Signature Load (0.2ms)   SELECT * FROM `signature` WHERE (`signature`.`sig_id` = 15) 
      Iphdr Load (0.2ms)   SELECT * FROM `iphdr` WHERE ((`iphdr`.`sid`=7 AND `iphdr`.`cid`=469)) 
      CACHE (0.0ms)   SELECT * FROM `sensor` WHERE (`sensor`.`sid` = 7) 
      SigClass Columns (1.5ms)   SHOW FIELDS FROM `sig_class`
      SigClass Load (1.1ms)   SELECT * FROM `sig_class` WHERE (`sig_class`.`sig_class_id` = 2) 
      CACHE (0.0ms)   SELECT * FROM `signature` WHERE (`signature`.`sig_id` = 29) 
      Iphdr Load (0.2ms)   SELECT * FROM `iphdr` WHERE ((`iphdr`.`sid`=7 AND `iphdr`.`cid`=468)) 
      CACHE (0.0ms)   SELECT * FROM `sensor` WHERE (`sensor`.`sid` = 7) 
      CACHE (0.0ms)   SELECT * FROM `signature` WHERE (`signature`.`sig_id` = 15) 
      Iphdr Load (0.2ms)   SELECT * FROM `iphdr` WHERE ((`iphdr`.`sid`=7 AND `iphdr`.`cid`=467)) 
      CACHE (0.0ms)   SELECT * FROM `sensor` WHERE (`sensor`.`sid` = 7) 
      CACHE (0.0ms)   SELECT * FROM `sig_class` WHERE (`sig_class`.`sig_class_id` = 2) 
    Rendered events/_event (36.2ms)
    Rendered events/_event (3.7ms)
    Rendered events/_event (2.9ms)
    Rendered events/_event (2.8ms)
    Rendered events/_event (2.8ms)
    Rendered layouts/_header (0.2ms)
    Rendered layouts/_footer (0.1ms)
    Completed in 57ms (View: 33, DB: 22) | 200 OK [http://0.0.0.0/]
    
  • Mislav

    Mislav June 26th, 2009 @ 12:46 PM

    I don't understand. First of all, you said:

    Event.all.paginate(...)
    

    This is wrong. It should be Event.paginate(), because Event.all loads all the records and you don't want that. However, from your log I see that indeed Event.paginate is executed, so I'm guessing you pasted wrong.

    Second of all, everything seems in order. These look perfectly OK:

    SELECT * FROM `event` ORDER BY timestamp DESC LIMIT 0, 5
    SELECT count(*) AS count_all FROM `event`
    

    The rest of the queries are triggered in your templates while the loop goes through all 5 records. This can be kinda slow, and you can improve the load times with eager includes:

    Event.paginate(:include => [:signature, ...])
    

    Looking from what you pasted I don't see obvious errors and where at all are composite primary keys used.

  • mephux

    mephux June 26th, 2009 @ 11:37 PM

    You are correct the issue was unrelated. I am sorry to have wasted your time and thank you for the eager tip. This ticket is good to close.

  • Mislav

    Mislav June 27th, 2009 @ 04:02 PM

    • State changed from “open” to “invalid”

    No prob :) Your duty is to report bugs and mine to find out where they are.

    Keep being involved and projects maintainers will always be grateful.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket

Tags

Pages