jeudi 29 octobre 2015

best way to optimize this sqlite database

hello i have a sqlite database contains one table with this structure

CREATE TABLE stats (
    day_date          DATE     NOT NULL,
    listing_id        INT (10) NOT NULL,
    cat_id            INT (4)  DEFAULT (0),
    source_id         INT (4)  DEFAULT (0),
    views             INT (8)  NOT NULL
                               DEFAULT (0),
    views_counter     INT (8)  NOT NULL
                               DEFAULT (0),
    bot_views         INT (8)  NOT NULL
                               DEFAULT (0),
    bot_views_counter INT (8)  DEFAULT (0) 
                               NOT NULL,
    PRIMARY KEY (
        day_date
    )
);

and this is the file

this database may contains 500,000 rows, so i want to make some indices to make the query fast

the query i use on this db is

SELECT listing_id,SUM(views) AS total_views FROM stats WHERE day_date IN("11-10-2015","12-10-2015","13-10-2015","14-10-2015","15-10-2015","16-10-2015","17-10-2015") group by listing_id ORDER BY total_views DESC 

i use the range on day_date to get one day,one week entires,

i have some ideas , what about make a new column named 'week' contains the week number of the year and add index on it so if i want a day i will use only one value on day_date ,if i want a week i will use week column ... is that right?

Aucun commentaire:

Enregistrer un commentaire