Commit 7ab03c87 authored by Dmitry Shelepnev's avatar Dmitry Shelepnev
Browse files

Optimize database

parent 20b7789b
Loading
Loading
Loading
Loading
−5.46 KiB

File deleted.

−5.67 KiB

File deleted.

−9.36 KiB

File deleted.

+10 −10
Original line number Diff line number Diff line
@@ -238,10 +238,10 @@ class CatalogsFeed(AuthFeed):

    def items(self, obj):
        cat, current_page = obj
        catalogs_list = Catalog.objects.filter(parent=cat).order_by("cat_type","cat_name")
        catalogs_list = Catalog.objects.filter(parent=cat).order_by("cat_name", "cat_type")
        # prefetch_related on sqlite on items >999 therow error "too many SQL variables"
        #books_list = Book.objects.filter(catalog=cat).prefetch_related('authors','genres','series').order_by("title")
        books_list = Book.objects.filter(catalog=cat).order_by("title")
        books_list = Book.objects.filter(catalog=cat).order_by("search_title")
        union_list = list(chain(catalogs_list,books_list))
        paginator = Paginator(union_list,settings.MAXITEMS)
        try:
@@ -350,29 +350,29 @@ class SearchBooksFeed(AuthFeed):
        # Поиск книг по подсроке
        if  searchtype == 'm':
            #books = Book.objects.extra(where=["upper(title) like %s"], params=["%%%s%%"%searchterms.upper()]).order_by('title','-docdate')
            books = Book.objects.filter(search_title__contains=searchterms.upper()).order_by('title','-docdate')
            books = Book.objects.filter(search_title__contains=searchterms.upper()).order_by('search_title','-docdate')
        # Поиск книг по начальной подстроке
        elif searchtype == 'b':
            #books = Book.objects.extra(where=["upper(title) like %s"], params=["%s%%"%searchterms.upper()]).order_by('title','-docdate')
            books = Book.objects.filter(search_title__startwith=searchterms.upper()).order_by('title','-docdate')
            books = Book.objects.filter(search_title__startwith=searchterms.upper()).order_by('search_title','-docdate')
        # Поиск книг по точному совпадению наименования
        elif searchtype == 'e':
            #books = Book.objects.extra(where=["upper(title)=%s"], params=["%s"%searchterms.upper()]).order_by('title','-docdate')
            books = Book.objects.filter(search_title=searchterms.upper()).order_by('title','-docdate')
            books = Book.objects.filter(search_title=searchterms.upper()).order_by('search_title','-docdate')
        # Поиск книг по автору
        elif searchtype == 'a':
            try:
                author_id = int(searchterms)
            except:
                author_id = 0
            books = Book.objects.filter(authors=author_id).order_by('title','-docdate')
            books = Book.objects.filter(authors=author_id).order_by('search_title','-docdate')
        # Поиск книг по серии
        elif searchtype == 's':
            try:
                ser_id = int(searchterms)
            except:
                ser_id = 0
            books = Book.objects.filter(series=ser_id).order_by('title','-docdate')    
            books = Book.objects.filter(series=ser_id).order_by('search_title','-docdate')    
        # Поиск книг по автору и серии
        elif searchtype == "as":
            try:
@@ -381,14 +381,14 @@ class SearchBooksFeed(AuthFeed):
            except:
                ser_id = 0
                author_id = 0 
            books = Book.objects.filter(authors=author_id, series=ser_id if ser_id else None).order_by('title','-docdate')
            books = Book.objects.filter(authors=author_id, series=ser_id if ser_id else None).order_by('search_title','-docdate')
        # Поиск книг по жанру
        elif searchtype == 'g':
            try:
                genre_id = int(searchterms)
            except:
                genre_id = 0
            books = Book.objects.filter(genres=genre_id).order_by('title','-docdate')    
            books = Book.objects.filter(genres=genre_id).order_by('search_title','-docdate')    
        # Поиск книг на книжной полке            
        elif searchtype == 'u':
            if settings.AUTH:
@@ -399,7 +399,7 @@ class SearchBooksFeed(AuthFeed):
        elif searchtype == 'd':
            book_id = int(searchterms)
            mbook = Book.objects.get(id=book_id)
            books = Book.objects.filter(title__iexact=mbook.title, authors__in=mbook.authors.all()).exclude(id=book_id).order_by('title','-docdate')
            books = Book.objects.filter(title__iexact=mbook.title, authors__in=mbook.authors.all()).exclude(id=book_id).order_by('search_title','-docdate')
                    
        # prefetch_related on sqlite on items >999 therow error "too many SQL variables"
        #if len(books)>0:            
+12 −29
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-15 10:44
# Generated by Django 1.10.3 on 2016-11-15 12:51
from __future__ import unicode_literals

from django.conf import settings
@@ -22,8 +22,8 @@ class Migration(migrations.Migration):
            name='Author',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('first_name', models.CharField(max_length=64)),
                ('last_name', models.CharField(max_length=64)),
                ('full_name', models.CharField(db_index=True, default=None, max_length=128)),
                ('search_full_name', models.CharField(db_index=True, default=None, max_length=128)),
                ('lang_code', models.IntegerField(db_index=True, default=9)),
            ],
        ),
@@ -46,16 +46,17 @@ class Migration(migrations.Migration):
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('filename', models.CharField(db_index=True, max_length=256)),
                ('path', models.CharField(db_index=True, max_length=1000)),
                ('filesize', models.IntegerField(default=0)),
                ('format', models.CharField(max_length=8)),
                ('filesize', models.IntegerField(db_index=True, default=0)),
                ('format', models.CharField(db_index=True, max_length=8)),
                ('cat_type', models.IntegerField(default=0)),
                ('registerdate', models.DateTimeField(db_index=True, default=django.utils.timezone.now)),
                ('docdate', models.CharField(max_length=32)),
                ('lang', models.CharField(max_length=16)),
                ('title', models.CharField(max_length=256)),
                ('title', models.CharField(db_index=True, max_length=256)),
                ('search_title', models.CharField(db_index=True, default=None, max_length=256)),
                ('annotation', models.CharField(max_length=10000)),
                ('lang_code', models.IntegerField(db_index=True, default=9)),
                ('avail', models.IntegerField(default=0)),
                ('avail', models.IntegerField(db_index=True, default=0)),
                ('authors', models.ManyToManyField(through='opds_catalog.bauthor', to='opds_catalog.Author')),
            ],
        ),
@@ -83,6 +84,7 @@ class Migration(migrations.Migration):
                ('cat_name', models.CharField(db_index=True, max_length=128)),
                ('path', models.CharField(db_index=True, max_length=1000)),
                ('cat_type', models.IntegerField(default=0)),
                ('cat_size', models.IntegerField(default=0, null=True)),
                ('parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='opds_catalog.Catalog')),
            ],
        ),
@@ -102,8 +104,8 @@ class Migration(migrations.Migration):
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('genre', models.CharField(db_index=True, max_length=32)),
                ('section', models.CharField(max_length=64)),
                ('subsection', models.CharField(max_length=100)),
                ('section', models.CharField(db_index=True, max_length=64)),
                ('subsection', models.CharField(db_index=True, max_length=100)),
            ],
        ),
        migrations.CreateModel(
@@ -111,6 +113,7 @@ class Migration(migrations.Migration):
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ser', models.CharField(db_index=True, max_length=80)),
                ('search_ser', models.CharField(db_index=True, default=None, max_length=80)),
                ('lang_code', models.IntegerField(db_index=True, default=9)),
            ],
        ),
@@ -149,24 +152,4 @@ class Migration(migrations.Migration):
            name='book',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='opds_catalog.Book'),
        ),
        migrations.AlterIndexTogether(
            name='author',
            index_together=set([('last_name', 'first_name')]),
        ),
        migrations.AlterIndexTogether(
            name='bseries',
            index_together=set([('book', 'ser')]),
        ),
        migrations.AlterIndexTogether(
            name='book',
            index_together=set([('title', 'format', 'filesize'), ('avail',)]),
        ),
        migrations.AlterIndexTogether(
            name='bgenre',
            index_together=set([('book', 'genre')]),
        ),
        migrations.AlterIndexTogether(
            name='bauthor',
            index_together=set([('book', 'author')]),
        ),
    ]
Loading