Question: I have the sql query below. I want to take the results and insert it into another table using the insert below. Is this a good choice for a stored procedure.

It doesn't need to run very often, so no need to attach it to a trigger... I'll probably just write a php script to trigger it via an hourly cron job. (or maybe I can trigger it via command line)

So, am I barking up the wrong tree on this one? Thanks Oh, I'll accept any helpful answer.

```
 SELECT
  COUNT(*) AS thecount,
  MAX(datetime_acc) AS DATE,
  u.created_usr,
  @payout:=IF(u.created_usr < '2011-01-24',20,10) AS payout,
  level_usr,
  @uid_usr:=p.uid_usr,
  @affiliate:=u.affiliate_aff,
  created_usr,
  firstname_usr, 
  lastname_usr,
  contact_aff,
  c.id_com

FROM payment_acc p
 LEFT JOIN users_usr u ON p.uid_usr = u.id_usr 
 LEFT JOIN commissions_com c ON c.uid_usr = u.id_usr
 LEFT JOIN affiliate_aff a ON a.code_aff = u.affiliate_aff

WHERE p.type_acc = 'monthly payment'
AND affiliate_aff IS NOT NULL
GROUP BY p.uid_usr
HAVING thecount > 1

ORDER BY affiliate_aff

```

The INSERT:

```
Insert into commissions_com 
    date_generated_com,
    amount_com,
    uid_usr, 
    code_aff, 
    status_com
values
   (NOW(),
    @payout,
    @uid_usr,
    @code_aff,
    'new')

```

Answer: 
If you transaction log has grown so much is such a short time this means that a lot of statements that make data or structure changes have been executed. If your database works with large blob records you can try looking there first. 

Profiler won't help you much in finding out what happened previously but it can help you if this is still going on.

If you want to read into transaction log, you will need a 3rd party [transaction log reader](http://www.apexsql.com/sql_tools_log.aspx). The best solution on the market is [ApexSQL Log](http://www.apexsql.com/sql_tools_log.aspx) which saved me couple times in similar situations. 

However, if your database is running on sql server 2000, you can try to use SQL Log Rescue from Red Gate cause it's free. Thrid solution is to try and find Lumigent Log Explorer (product is discountinued but maybe you can find it somewhere online).

Try'em all and see which one works better for you.
You can use sql server profiler which show every transaction executed and it's start time and end time and many things and i think you can see what causing your problem.
I hope this help you.