diff s4-funcs.sh @ 471:1dacdf998892

Speed up new article listing
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 28 Aug 2017 22:51:45 +0859
parents f095f639e5bf
children 38bf8d300b12
line wrap: on
line diff
--- a/s4-funcs.sh	Thu Aug 24 07:44:38 2017 +0859
+++ b/s4-funcs.sh	Mon Aug 28 22:51:45 2017 +0859
@@ -31,6 +31,7 @@
 ### maximagexy=400x400
 file_accept='accept="image/*,text/*,audio/*,application/vnd.oasis.*,application/pdf,application/x-*"'
 blogreadflagrowid=0
+blogcutoffflagrowid=-1
 querylog=$tmpdir/query.log
 
 tconfs=""
@@ -506,7 +507,7 @@
 }
 acclog() (
   # $1=table, $2=rowid
-  n=${2%%[!0-9]*}	# Remove non-digit chars from $2(should be rowid)
+  n=${2%%[!-0-9]*}	# Remove non-digit chars from $2(should be rowid)
   if [ -n "$n" ]; then
     now=`date +"%F %T"`
     #query "replace into acclog values('$user', '$1', '$n', '$now');"
@@ -1542,51 +1543,36 @@
 EOF
 }
 listnewblogsql() { # $1=user
-  deftime=`query "SELECT coalesce((SELECT time FROM acclog
+  deftime=`query "SELECT coalesce((SELECT max(time) FROM acclog
 		  	 	   WHERE user='$user'
-				    AND tblrowid=$blogreadflagrowid),
+				    AND tblrowid IN
+				      ($blogreadflagrowid,
+				       $blogcutoffflagrowid)),
 				  "0");"`
   cat<<EOF
 `sql4interestblogs`
 WITH article_ctime as (
- SELECT id,blogid,author,val ctime
+ SELECT id,blogid,author,max(val) ctime
  FROM   article join article_s s using(id)
- WHERE  s.key='ctime'
+ WHERE  s.key='ctime' AND s.val > '$deftime'
+ GROUP BY id
 ), blog_title_owner as (
  SELECT blg.rid brid, id,
         max(case key when 'title' then val end) title,
         max(case key when 'owner' then val end) owner
  FROM interestblogs blg, blog_s using(id) group by id
-), visited AS (
- SELECT b.id id, brid, b.owner owner, b.title title, ctime, ac.author author
- FROM blog_title_owner b, article_ctime ac
-      JOIN acclog al
-      ON b.id=ac.blogid
-                AND al.tbl='blog'
-                AND al.tblrowid=brid
-                AND al.user='$user'
-		AND al.time < ctime
-		AND '$deftime' < ctime
-), unvisited as (
- SELECT b.id id, brid, b.owner owner, b.title title, ctime, ac.author author
- FROM blog_title_owner b, article_ctime ac
- ON b.id=ac.blogid
- WHERE brid NOT IN (SELECT tblrowid FROM acclog
-                   WHERE tbl='blog' AND user='$user')
-       AND ctime > '$deftime'
 ), blogall as (
- /* ---------------------------------------
-	Collect new articles with dividing them into visited and unvisited
-	separately, because constructing joined table of artice X acclog
-	tends to become HUGE combinations.
-    --------------------------------------- */
- SELECT * FROM visited UNION SELECT * FROM unvisited
+ SELECT * FROM blog_title_owner b JOIN article_ctime ac ON b.id=ac.blogid
 ), news as (
- select bl.brid brid, bl.title, bl.id blid, ctime,
-        count(bl.id) "新着", bl.owner, bl.author
- from blogall bl /* left join a_u l
- on bl.brid=l.tblrowid */
- group by bl.id order by ctime desc,"新着" desc, bl.id
+ SELECT brid, bl.id blid, bl.title, ctime,
+ 	coalesce(al.time, '$deftime') atime,
+        count(bl.id) "新着", bl.author
+ FROM blogall bl
+      LEFT JOIN
+      (SELECT * FROM acclog WHERE user='$user' AND tbl='blog') al
+      ON bl.brid=al.tblrowid
+ WHERE atime < bl.ctime
+ GROUP by bl.id ORDER BY ctime desc,"新着" desc, bl.id
  LIMIT 10
 ) SELECT brid LINK, "新着",
        (SELECT count(*) FROM article WHERE blogid=blid) "総数",
@@ -1835,6 +1821,7 @@
     fi
     # 2016-02-19 Counting NEWS without using dumptable.
     sql=`listnewblogsql "$user"`
+    # echo "$sql" > tmp/listnew
     new10=`DT_SQL="$sql" DT_VIEW=replyblog dumptable html blog`
     cont=`echo "$new10"|grep "^<TR>"|wc -l`
     cont=$((cont-1))
@@ -1851,6 +1838,8 @@
 	EOF
       echo "$new10 <!-- new10 -->"
       echo "</div>"
+    else			# If news is 0, set log cut off flag
+      acclog blog $blogcutoffflagrowid	# for speed
     fi
   else				# Not My Home ($user != $uname)
     : # DT_SQL=

yatex.org