[Commits] Rev 2875: Fix problem of making on-disk cache table. in file:///home/bell/maria/bzr/work-maria-5.3-subquerycache/

sanja at askmonty.org sanja at askmonty.org
Mon Jan 17 21:54:32 EET 2011


At file:///home/bell/maria/bzr/work-maria-5.3-subquerycache/

------------------------------------------------------------
revno: 2875
revision-id: sanja at askmonty.org-20110117195431-i50nif49w9grzohk
parent: igor at askmonty.org-20101230021403-ly4ku1yq675l5d6b
committer: sanja at askmonty.org
branch nick: work-maria-5.3-subquerycache
timestamp: Mon 2011-01-17 21:54:31 +0200
message:
  Fix problem of making on-disk cache table.
  Removed 'uniques=1' which lead to creating unneeded unique index creation.
-------------- next part --------------
=== modified file 'sql/sql_expression_cache.cc'
--- a/sql/sql_expression_cache.cc	2010-11-02 09:47:36 +0000
+++ b/sql/sql_expression_cache.cc	2011-01-17 19:54:31 +0000
@@ -1,6 +1,10 @@
 
 #include "mysql_priv.h"
 #include "sql_select.h"
+#include <my_pthread.h>
+
+/* hitrate over 50% is OK to leave built in-memory temporary table */
+#define EXPRASSION_TMPTABLE_ALLOWED_HITRATE 50
 
 /*
   Expression cache is used only for caching subqueries now, so its statistic
@@ -11,8 +15,8 @@ ulong subquery_cache_miss, subquery_cach
 Expression_cache_tmptable::Expression_cache_tmptable(THD *thd,
                                                  List<Item*> &dependants,
                                                  Item *value)
-  :cache_table(NULL), table_thd(thd), list(&dependants), val(value),
-   inited (0)
+  :hit(0), miss(0), frozen(0), cache_table(NULL), table_thd(thd),
+   list(&dependants), val(value), inited(0)
 {
   DBUG_ENTER("Expression_cache_tmptable::Expression_cache_tmptable");
   DBUG_VOID_RETURN;
@@ -85,6 +89,7 @@ void Expression_cache_tmptable::init()
   /* postpone table creation to index description */
   cache_table_param.skip_create_table= 1;
   cache_table= NULL;
+  frozen= 0;
 
   items.push_front(val);
 
@@ -123,7 +128,6 @@ void Expression_cache_tmptable::init()
     goto error;
   }
   cache_table->s->keys= 1;
-  cache_table->s->uniques= 1;
   ref.null_rejecting= 1;
   ref.disable_cache= FALSE;
   ref.has_record= 0;
@@ -194,11 +198,13 @@ Expression_cache::result Expression_cach
       DBUG_RETURN(ERROR);
     if (res)
     {
-      subquery_cache_miss++;
+      statistic_increment(subquery_cache_miss, &LOCK_status);
+      miss++;
       DBUG_RETURN(MISS);
     }
 
-    subquery_cache_hit++;
+    statistic_increment(subquery_cache_hit, &LOCK_status);
+    hit++;
     *value= cached_result;
     DBUG_RETURN(Expression_cache::HIT);
   }
@@ -225,7 +231,7 @@ my_bool Expression_cache_tmptable::put_v
   DBUG_ENTER("Expression_cache_tmptable::put_value");
   DBUG_ASSERT(inited);
 
-  if (!cache_table)
+  if (!cache_table || frozen)
   {
     DBUG_PRINT("info", ("No table so behave as we successfully put value"));
     DBUG_RETURN(FALSE);
@@ -238,6 +244,8 @@ my_bool Expression_cache_tmptable::put_v
 
   if ((error= cache_table->file->ha_write_row(cache_table->record[0])))
   {
+    /* TODO: add estimation if ondisk table is efficient */
+#ifdef OVERFLOW_CACHE_TO_DISK_BASED_TABLE
     /* create_myisam_from_heap will generate error if needed */
     if (cache_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
         create_internal_tmp_table_from_heap(table_thd, cache_table,
@@ -245,6 +253,19 @@ my_bool Expression_cache_tmptable::put_v
                                             &cache_table_param.recinfo,
                                             error, 1))
       goto err;
+#else
+    if (cache_table->file->is_fatal_error(error, HA_CHECK_DUP))
+      goto err;
+    /*
+      should we leave the table
+
+      + 1 used just to avoid division on 0, for big values it dos not matter.
+    */
+    if ((((ulonglong)hit) * 100) / (hit + miss + 1) <
+        EXPRASSION_TMPTABLE_ALLOWED_HITRATE)
+      goto err;                              // remove the temporary table
+    frozen= TRUE;                            // leave the temporary table
+#endif
   }
   cache_table->status= 0; /* cache_table->record contains an existed record */
   ref.has_record= TRUE; /* the same as above */

=== modified file 'sql/sql_expression_cache.h'
--- a/sql/sql_expression_cache.h	2010-10-27 03:03:59 +0000
+++ b/sql/sql_expression_cache.h	2011-01-17 19:54:31 +0000
@@ -19,7 +19,7 @@ class Expression_cache :public Sql_alloc
 public:
   enum result {ERROR, HIT, MISS};
 
-  Expression_cache(){};
+  Expression_cache() {};
   virtual ~Expression_cache() {};
   /**
     Shall check the presence of expression value in the cache for a given
@@ -50,6 +50,8 @@ class Item_field;
 
 class Expression_cache_tmptable :public Expression_cache
 {
+  ulong hit, miss;
+  bool frozen;
 public:
   Expression_cache_tmptable(THD *thd, List<Item*> &dependants, Item *value);
   virtual ~Expression_cache_tmptable();



More information about the commits mailing list