changeset 257:8098c84efab0

SQL optimizations. * Reduced the size of the `url` column to make it unique, along with the `path` column. * Removed an unecessary lazy loading of some DB object property.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Apr 2014 21:05:25 -0700
parents 5667f11fd7c9
children 9416b2e6890c
files wikked/db/sql.py
diffstat 1 files changed, 10 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/db/sql.py	Thu Apr 03 20:05:13 2014 -0700
+++ b/wikked/db/sql.py	Thu Apr 03 21:05:25 2014 -0700
@@ -12,7 +12,7 @@
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.orm import (
     scoped_session, sessionmaker,
-    relationship, backref, load_only, subqueryload)
+    relationship, backref, load_only, subqueryload, joinedload)
 from sqlalchemy.orm.exc import NoResultFound
 from wikked.db.base import Database
 from wikked.page import Page, PageData, FileSystemPage
@@ -29,11 +29,11 @@
 
     id = Column(Integer, primary_key=True)
     time = Column(DateTime)
-    # Most browsers/search engines won't accept URLs longer than ~2000 chars.
-    url = Column(String(2048))
     # In the spirit of cross-platformness we let Windows' suckiness dictacte
-    # this length.
-    path = Column(String(260))
+    # this length (but it's good because it makes those 2 columns short enough
+    # to be indexable by SQL).
+    url = Column(String(260), unique=True)
+    path = Column(String(260), unique=True)
     title = Column(UnicodeText)
     raw_text = Column(UnicodeText(length=2 ** 31))
     formatted_text = Column(UnicodeText(length=2 ** 31))
@@ -405,7 +405,7 @@
             return None
         return SQLDatabasePage(self, page, fields)
 
-    def _addFieldOptions(self, query, fields):
+    def _addFieldOptions(self, query, fields, use_joined=False):
         if fields is None:
             return query
 
@@ -428,7 +428,10 @@
             query = query.options(load_only(col))
             sqf = subqueryfields.get(f)
             if sqf:
-                query = query.options(subqueryload(sqf))
+                if use_joined:
+                    query = query.options(joinedload(sqf))
+                else:
+                    query = query.options(subqueryload(sqf))
         return query
 
     def _addPage(self, page):
@@ -503,12 +506,6 @@
 
         if fields is None or ('meta' in fields or 'links' in fields or
                               'text' in fields):
-            if not db_obj.is_ready:
-                raise Exception(
-                    "Requested extended data for page '%s' "
-                    "but data is not cached in the SQL database." % (
-                        data.url or data._db_id))
-
             if fields is None or 'text' in fields:
                 data.text = db_obj.ready_text