I'm working on a database application with Perl and Postgresql, using DBI and Mojolicous. The main DB table is very long and I'm trying to come up with a way to reduce the data. At first I tried a pure SQL approach, but the results were disappointingly slow. Now I'm considering a Perl approach. If you have the patience please read on.

Each line in the table is a log. There will be many rows that are the same except for the timestamp. As the data ages, I want to cull the rows. I want to group records that have the same combination of class, ip_address, and hostname, and keep the highest timestamp for each day from each group.

Table "public.agent_log"

class | text | hostname | text | ip_address | text | promise_handle | text | promiser | text | promisee | text | policy_server | text | rowId | integer | \ not null default nextval('"agent_log_rowId_seq"'::regclass) timestamp | timestamp with time zone | promise_outcome | text | Indexes: "primary_key" PRIMARY KEY, btree ("rowId") "client_by_timestamp" btree ("timestamp", class)

Example data

2014-01-22T13:44:00 any 192.168.0.1 moon.example.com ... 2014-01-22T14:44:00 any 192.168.0.1 moon.example.com ... KEEP 2014-01-22T14:44:00 any 192.168.0.2 mars.example.com ... KEEP 2014-01-22T13:44:00 any 192.168.0.2 mars.example.com ... 2014-01-23T13:44:00 any 192.168.0.1 moon.example.com ... 2014-01-23T14:44:00 any 192.168.0.1 moon.example.com ... KEEP 2014-01-23T14:44:00 any 192.168.0.2 mars.example.com ... KEEP 2014-01-23T13:44:00 any 192.168.0.2 mars.example.com ... 2014-01-22T13:44:00 cpu_1 192.168.0.1 moon.example.com ... 2014-01-22T14:44:00 cpu_1 192.168.0.1 moon.example.com ... KEEP 2014-01-22T14:44:00 cpu_1 192.168.0.2 mars.example.com ... KEEP 2014-01-22T13:44:00 cpu_1 192.168.0.2 mars.example.com ... 2014-01-23T13:44:00 cpu_1 192.168.0.1 moon.example.com ... 2014-01-23T14:44:00 cpu_1 192.168.0.1 moon.example.com ... KEEP 2014-01-23T14:44:00 cpu_1 192.168.0.2 mars.example.com ... KEEP 2014-01-23T13:44:00 cpu_1 192.168.0.2 mars.example.com ...

I tried aggregating the data with

SELECT class, max(timestamp) as timestamp, hostname, ip_address, promise_handle, promiser, promisee, policy_server, promise_outcome FROM agent_log WHERE timestamp < now() - interval '7 days' GROUP BY class, DATE_TRUNC( 'day', timestamp), hostname, ip_address, promise_handle, promiser, promisee, policy_server, promise_outcome

I wrapped that in a query that didn't quite work, but was essentially DELETE * FROM agent_log WHERE NOT IN... It was painfully slow.Now I'm wondering if I can do this with Perl instead. Using cursor I could fetch rows, group them, and delete the lower date ones. Is it likely that using Perl in anyway could be faster than raw SQL? What would you do?

Neil Watson
watson-wilson.ca


In reply to Help on selecting and deleting many records in a DB. by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.