Always index your tables. Always

ireadwhite Always index your tables. Always
This is going to be brief one: iRead.ro, one of the projects I’m working on during my spare time, was giving me major headaches lately - 2 weeks ago, it took like 15 seconds to render a page. In the last couple of days, it was taking somewhere around 100 seconds to render it; obviously too much for the nginx server that was running my two mongrels.

I *had* indexed my tables, by id, url and several other fields. Turns out, they weren’t indexed with the right columns.


I went into the console and tried running the simple query used by the controller, in order to retrieve the FeedItems (where FeedItem < Item) between two boundary dates. Yep, the +100 seconds were there. Although the over 32000 items in the table were obviously not a *huge* number, perhaps their size(and the limits of my slicehost RAM) was the problem.

Quick fix:


./script/generate migration AddExtraIndex
class AddExtraIndex < ActiveRecord::Migration
  def self.up
    add_index :items, :type
    add_index :items, :date
  end

  def self.down
    #...
  end
end

Ran the migration and… here you go. iRead now renders the pages in around 3 seconds (without any caching whatsoever). Enjoy!

Articole inrudite

4 comentarii

  1. Publicat August 30, 2008 la 7:06 pm | Permalink

    “…Safari could not open the page “http://iread.ro/” because the server is not responding.”
    Iar asta e asa cam de foo’… saptamana?
    Hai, inteleg ca nu mai ai entuziasm/te-ai plictisit… da’ macar anunta.

  2. Publicat August 30, 2008 la 7:08 pm | Permalink

    e drept ca am fost prins cu altele, dar ai dreptate- trebuie sa ma pun cu burta pe treaba.
    incerc sa vad azi ce se poate face..

  3. Publicat September 2, 2008 la 1:49 am | Permalink

    Alex … ai un talent de a te imprastia in toate directiile (si proiectele) ceva de speriat! Mai ca ma ajungi din urma!
    D-aia si dormi prost noaptea.
    Sugestia mea (sincera, fara kkaturi idioate) :
    iread e ceva bun, la care chiar ai muncit, si pe care te pregatesti sa-l ingropi in bezna… degeaba!
    Fa-l sa MEARGA cum trebuie, pune dracu’ ceva google adsense, si mai te uiti o data pe luna la el. Nu cred ca cineva o sa se bosumfle ca pui reclame pe un sait la care tu platesti hosting si banda.
    Cum am mai zis si in alta parte, ma enervezi! Nu fiindca te-ai delasat (asta o inteleg) dar fiindca nu TERMINI ce-ai inceput! Rusine, alea.
    Uite si opinia mea (pentru cat valoreaza … ceva gen 2 lei vechi)
    Din toate agregatoarele pe care le-am vazut, al tau era cel mai bun…
    Hai, Alex, chiar vrei sa ma milogesc?
    Pune mana, o ora pe zi… intr-o saptamana, n-ai ce alege. :)

  4. Publicat September 2, 2008 la 5:39 pm | Permalink

    Asa, vezi? Deci, se poate… :)
    Ia sa prestez si eu ce propovaduiesc … :)

Un trackback

  1. De Recent Links Tagged With "sqlite" - JabberTags pe September 27, 2008 la 7:04 pm

    [...] public links >> sqlite Always index your tables. Always Saved by energia on Thu 25-9-2008 PostgreSQL to SQLite Saved by khedron on Thu [...]

Lasa un comentariu

Your email is never shared. Campurile obligatorii sunt marcate cu *

*
*