Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

per_page and composite_primary_keys

#272

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.

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

State: invalid
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. mephux
    mephux

    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.

    June 25th, 2009 @ 09:59 AM

  2. Mislav
    Mislav
    • 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

    June 25th, 2009 @ 05:43 PM

  3. mephux
    mephux

    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.

    June 25th, 2009 @ 10:37 PM

  4. mephux
    mephux

    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/]
    

    June 25th, 2009 @ 10:41 PM

  5. Mislav
    Mislav

    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.

    June 26th, 2009 @ 12:46 PM

  6. mephux
    mephux

    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.

    June 26th, 2009 @ 11:37 PM

  7. Mislav
    Mislav
    • 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.

    June 27th, 2009 @ 04:02 PM

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.