title: Convert into SQL a Magento Model based script
body: <p>Is there any way to convert a script that use magento model and collection in a script that use direct SQL.</p>
  
  <p>I know it is possible as quite everything :), The question is about an easy and intuitive way that let me get the query from my script and then reuse in a direct DB script.</p>
  
  <p>Why ? I hope to have a faster script.</p>
  
  <p>Code example ? </p>
  
  <p>Suppose this script:</p>
  
  <pre><code>function getProd() {
  
  $collection = Mage::getModel('catalog/product')
          -&gt;getCollection()
          -&gt;addTierPriceData();
  
  return $collection;
  }
  function getTier($prod) {
  
  $t_1 = false;
  
  $_tierPrices = $prod-&gt;getTierPrice();
  
  if ($_tierPrices) {
  
      $_firstTier = array_slice($_tierPrices, 0, 1);
  
      if (isset($_firstTier[0]))
          $t_1 = $_firstTier[0]['price'];
  }
  
  return $t_1;
  }
  
  function exec() {
  
  $collection = getProd();
  
  $i = 0;
  
  foreach ($collection as $prod) {
  
      $class = ($i % 2 == 0) ? '' : 'class="odd"';
      $t1 = 0;
      if ($t1 = getTier($prod)) {
  
              $prod = mage::getModel('catalog/product')-&gt;load($prod-&gt;getId());
  
              $prod-&gt;setPrice($t1);
              $prod-&gt;save();
              if ($safe)
                  set_time_limit(60);
          }
      }
  }
  
  exec();
  </code></pre>
  
  <p><strong>Basically I would like to process all products that has a tier price and substitute the main price of the product with the tier price.</strong></p>
  
  <p>The script takes more than 9hr on a 12000 products so I guess an SQL approach would be faster but require me to study DB structure.</p>
  
  <p>So If any one has a good approach to convert this in a PHP/SQL script would be great ( something more that only print the collection query )</p>