Commit 692c66b0 authored by mitshel's avatar mitshel
Browse files

Изменен запрос получения количества новых книг (увеличение скорости запроса)

Для выполнения запроса написан хранимая процедура sp_newinfo
parent 74bac30a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ alphabet_menu = yes
# new_period = N - количество суток в течении которых добавленные книги считаются новыми, по умолчанию 7 дней
# Если new_period=0, то меню новинок не будет выводится вовсе
# по умолчанию new_period = 7
new_period = 7 
new_period = 1 

# Установка параметра book_shelf = yes приведет к появлению в основном меню Книжной полки для пользователя (если настроена авторизация web-сервером)
# Здесь будут запоминаться скачанные пользователем книги. Это удобно при использование нескольких устройств.
+19 −3
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ filesize INT not null DEFAULT 0,
format VARCHAR(8),
cat_id INT not null,
cat_type INT not null DEFAULT 0,
registerdate DATE not null,
registerdate TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
docdate VARCHAR(20),
favorite INT not null DEFAULT 0,
lang  VARCHAR(16),
@@ -108,6 +108,7 @@ insert into authors(author_id,last_name,first_name) values(1,"Неизвестн
commit;

DROP PROCEDURE IF EXISTS sp_update_dbl;
DROP PROCEDURE IF EXISTS sp_newinfo;
DELIMITER //

CREATE PROCEDURE sp_update_dbl()
@@ -138,8 +139,23 @@ BEGIN
  CLOSE cur;
END //

DELIMITER ;
CREATE PROCEDURE sp_newinfo(period INT)
BEGIN
  DECLARE min_book_id INT;

  select MIN(book_id) into min_book_id from books where registerdate>now()-INTERVAL period DAY;
  select 1 s, count(*) from books where book_id>=min_book_id and avail!=0 and doublicat=0
  union all
  select 2 s, count(*) from (select author_id from bauthors where book_id>=min_book_id group by author_id) a
  union all
  select 3 s, count(*) from (select genre_id from bgenres where book_id>=min_book_id group by genre_id) a
  union all
  select 4 s, count(*) from (select ser_id from bseries where book_id>=min_book_id group by ser_id) a
  order by s;

commit;
END //


DELIMITER ;
commit;
+20 −16
Original line number Diff line number Diff line
@@ -153,8 +153,8 @@ class opdsDatabase:
       doublicat=self.finddouble(title,format,size)
    else:
       doublicat=0
    sql_addbook=("insert into "+TBL_BOOKS+"(filename,path,cat_id,filesize,format,title,annotation,docdate,lang,cat_type,registerdate,doublicat,avail) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 2)")
    data_addbook=(name,path,cat_id,size,format,title,annotation,docdate,lang,archive,datetime.date.today(),doublicat)
    sql_addbook=("insert into "+TBL_BOOKS+"(filename,path,cat_id,filesize,format,title,annotation,docdate,lang,cat_type,doublicat,avail) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 2)")
    data_addbook=(name,path,cat_id,size,format,title,annotation,docdate,lang,archive,doublicat)
    cursor=self.cnx.cursor()
    cursor.execute(sql_addbook,data_addbook)
    book_id=cursor.lastrowid
@@ -690,20 +690,24 @@ class opdsDatabase:
    return rows

  def getnewinfo(self,doublicates=True,new_period=0):
    if doublicates: dstr=''
    else: dstr='and doublicat=0'

    if new_period==0: period=''
    else: period='and registerdate>now()-INTERVAL %s DAY'%new_period

    sql1="select 1 s, count(avail) from %s where avail!=0 %s %s"%(TBL_BOOKS,dstr,period)
    sql2="select 2 s, count(*) from (select b.author_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.author_id) a1"%(TBL_BAUTHORS, TBL_BOOKS, period)
    sql3="select 3 s, count(*) from (select b.genre_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.genre_id) a2"%(TBL_BGENRES, TBL_BOOKS, period)
    sql4="select 4 s, count(*) from (select b.ser_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.ser_id) a3"%(TBL_BSERIES, TBL_BOOKS, period)
    sql=sql1+" union all "+sql2+" union all "+sql3+" union all "+sql4+" order by s"
    cursor=self.cnx.cursor()
    cursor.execute(sql)
    rows=cursor.fetchall()
#    if doublicates: dstr=''
#    else: dstr='and doublicat=0'
#
#    if new_period==0: period=''
#    else: period='and registerdate>now()-INTERVAL %s DAY'%new_period
#
#    sql1="select 1 s, count(avail) from %s where avail!=0 %s %s"%(TBL_BOOKS,dstr,period)
#    sql2="select 2 s, count(*) from (select b.author_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.author_id) a1"%(TBL_BAUTHORS, TBL_BOOKS, period)
#    sql3="select 3 s, count(*) from (select b.genre_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.genre_id) a2"%(TBL_BGENRES, TBL_BOOKS, period)
#    sql4="select 4 s, count(*) from (select b.ser_id from %s b left join %s c on b.book_id=c.book_id where c.avail!=0 %s group by b.ser_id) a3"%(TBL_BSERIES, TBL_BOOKS, period)
#    sql=sql1+" union all "+sql2+" union all "+sql3+" union all "+sql4+" order by s"
    sql="call sp_newinfo(%s)"%new_period
    cursor=self.cnx.cursor()
#    cursor.execute(sql)
    cursor.callproc('sp_newinfo',(new_period,))
#    rows=cursor.fetchall()
    for results in cursor.stored_results():
        rows=results.fetchall()
    cursor.close
    return rows