diff --git a/node_gallery.inc b/node_gallery.inc
index f941582..98ce916 100644
--- a/node_gallery.inc
+++ b/node_gallery.inc
@@ -686,6 +686,31 @@ function node_gallery_get_image_count($gid, $reset = FALSE) {
 }
 
 /**
+ * Update image counts related to the Node Gallery node.
+ * 
+ * @param $node a gallery or an image node object
+ */
+function node_gallery_update_image_counts($node) {
+  if (in_array($node->type, (array)node_gallery_get_types('image'))) {
+    $gid = $node->gid;
+  }
+  elseif (in_array($node->type, (array)node_gallery_get_types('gallery'))) {
+    $gid = $node->nid;
+  }
+  else {
+    return;
+  }
+
+  $all = (int) db_result(db_query("SELECT COUNT(*) FROM {node_gallery_images} WHERE gid = %d", $gid));
+  $pub = (int) db_result(db_query(
+    "SELECT COUNT(*) FROM {node_gallery_images} ngi
+     INNER JOIN {node} n ON ngi.nid = n.nid
+     WHERE n.status = 1 AND ngi.gid = %d", $gid
+  ));
+  db_query("UPDATE {node_gallery_galleries} SET img_count = %d, pub_img_count = %d WHERE gid = %d", $all, $pub, $gid);
+}
+
+/**
  * Gets the first image nid within a gallery.
  * @param $gid
  *   The nid of the gallery to query.
diff --git a/node_gallery.install b/node_gallery.install
index 93ea0ed..b4785a3 100644
--- a/node_gallery.install
+++ b/node_gallery.install
@@ -103,12 +103,32 @@ function node_gallery_schema() {
         'default' => NULL,
         'description' => t('Node Id of the Cover Image')
       ),
+      'img_count' => array(
+        'type' => 'int',
+        'size' => 'small',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => t('Gallery images count.'),
+      ),
+      'pub_img_count' => array(
+        'type' => 'int',
+        'size' => 'small',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => t('Gallery published images count.'),
+      ),
     ),
     'primary key' => array('gid'),
     'unique keys' => array(
       // To quickly answer, is this image the cover image?
       'cover_image' => array('cover_image'),
     ),
+    'indexes' => array(
+      'img_count' => array('img_count'),
+      'pub_img_count' => array('pub_img_count')
+    )
   );
 
   return $schema;
@@ -860,3 +880,72 @@ function node_gallery_update_6304() {
   }
   return $ret;
 }
+
+/** 
+ * #1226982: add gallery image counts with indexes and create initial count values.
+ */
+function node_gallery_update_6305() {  
+  $ret = array();
+  
+  // Create count fields.
+  $fields = array(
+    'img_count' => array(
+      'type' => 'int',
+      'size' => 'small',
+      'unsigned' => TRUE,
+      'not null' => TRUE,
+      'default' => 0,
+      'description' => t('Gallery images count.'),
+    ),
+    'pub_img_count' => array(
+      'type' => 'int',
+      'size' => 'small',
+      'unsigned' => TRUE,
+      'not null' => TRUE,
+      'default' => 0,
+      'description' => t('Gallery published images count.'),
+    ),
+  );
+  foreach ($fields as $field => $spec) {
+    db_add_field($ret, 'node_gallery_galleries', $field, $spec);
+  }
+  
+  // Create indexes for searching and filtering using counts.
+  $indexes = array(
+    'img_count' => array('img_count'),
+    'pub_img_count' => array('pub_img_count')
+  );
+  foreach ($indexes as $name => $index) {
+    db_add_index($ret, 'node_gallery_galleries', $name, $index);
+  }
+  
+  // Create initial values for the counts. 
+  $counts = array();
+  $res = db_query("SELECT gid, count(*) AS c FROM {node_gallery_images} GROUP BY gid");
+  while ($row = db_fetch_array($res)) {
+    $counts[$row['gid']]['all'] = $row['c'];
+  }
+  $res = db_query("SELECT gid, count(*) AS c
+                   FROM {node_gallery_images} ngi
+                   INNER JOIN {node} n ON ngi.nid = n.nid
+                   WHERE n.status = 1 GROUP BY gid");
+  while ($row = db_fetch_array($res)) {
+    $counts[$row['gid']]['pub'] = (int) $row['c'];
+  }
+  foreach ($counts as $gid => $gc) {
+    db_query("UPDATE {node_gallery_galleries} SET img_count = %d, pub_img_count = %d WHERE gid = %d", $gc['all'], $gc['pub'], $gid);
+  }  
+  $processed_num = count($counts);
+  $ret[] = array(
+    'success' => TRUE,
+    'query' => format_plural(
+      $processed_num,
+      'Created initial image count values for 1 gallery.',
+      'Created initial image count values for !count galleries.',
+      array('!count' => $processed_num)
+    )
+  );
+  
+  drupal_flush_all_caches();
+  return $ret;
+}
diff --git a/node_gallery.module b/node_gallery.module
index 980f9ae..4ac5416 100644
--- a/node_gallery.module
+++ b/node_gallery.module
@@ -580,6 +580,7 @@ function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
           }
         }
         drupal_write_record('node_gallery_images', $node);
+        node_gallery_update_image_counts($node);
         if ($node->pluploaded == TRUE && variable_get('node_gallery_plupload_manage_images_integration', TRUE)) {
           if (!isset($_SESSION['node_gallery_plupload_nids'][$node->gid])) {
             $_SESSION['node_gallery_plupload_nids'][$node->gid] = array();
@@ -590,6 +591,7 @@ function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
       elseif (in_array($node->type, (array)node_gallery_get_types('gallery'))) {
         $node->gid = $node->nid;
         drupal_write_record('node_gallery_galleries', $node);
+        node_gallery_update_image_counts($node);
       }
       break;
     case 'update':
@@ -626,6 +628,7 @@ function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
         else {
           drupal_write_record('node_gallery_images', $node);
         }
+        node_gallery_update_image_counts($node);
       }
       elseif (in_array($node->type, (array)node_gallery_get_types('gallery'))) {
         $node->gid = $node->nid;
@@ -656,6 +659,7 @@ function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
         else {
           drupal_write_record('node_gallery_galleries', $node);
         }
+        node_gallery_update_image_counts($node);
         if (module_exists('pathauto')) {
           // Our tokens are all based off the gallery nid or title, no need for processing if those stay the same
           if ($node->title != $old_node->title || $node->gid != $old_node->gid) {
@@ -707,6 +711,7 @@ function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
       break;
     case 'delete':
       _node_gallery_delete($node);
+      node_gallery_update_image_counts($node);
       break;
   }
 }
diff --git a/views2inc/node_gallery.views.inc b/views2inc/node_gallery.views.inc
index b2e6857..66e93ee 100644
--- a/views2inc/node_gallery.views.inc
+++ b/views2inc/node_gallery.views.inc
@@ -105,13 +105,46 @@ function node_gallery_views_data() {
       'base field' => 'nid',
     ),
   );
+  // Note: "image_count" field alias is somewhat confusing, because the field 
+  // only counts published images.
+  // Keep it only to not break existing views.
   $data['node_gallery_galleries']['image_count'] = array(
+    'title' => t('Published Image Count'),
+    'help' => t('The number of published images in the gallery.'),
+    'real field' => 'pub_img_count',
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+      'numeric' => TRUE,
+      'validate type' => 'numeric',
+    ),
+  );
+  $data['node_gallery_galleries']['img_count'] = array(
     'title' => t('Image Count'),
-    'help' => t('The number of images in the gallery.'),
+    'help' => t('The number of images in the gallery, including unpublished.'),
     'field' => array(
-      'handler' => 'node_gallery_views_handler_image_count',
+      'handler' => 'views_handler_field_numeric',
       'click sortable' => TRUE,
-      'notafield' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+      'numeric' => TRUE,
+      'validate type' => 'numeric',
     ),
   );
   $data['node_gallery_galleries']['comments_count'] = array(
@@ -152,9 +185,6 @@ function node_gallery_views_handlers() {
       'path' => drupal_get_path('module', 'node_gallery') .'/views2inc',
     ),
     'handlers' => array(
-      'node_gallery_views_handler_image_count' => array(
-        'parent' => 'views_handler_field_numeric',
-      ),
       'node_gallery_views_handler_comments_count' => array(
         'parent' => 'views_handler_field_numeric',
       ),
diff --git a/views2inc/node_gallery_views_handler_image_count.inc b/views2inc/node_gallery_views_handler_image_count.inc
deleted file mode 100644
index dbdc814..0000000
--- a/views2inc/node_gallery_views_handler_image_count.inc
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-/**
- * @file
- * Creates an image count handler for galleries.
- */
-
-class node_gallery_views_handler_image_count extends views_handler_field_numeric {
-  function query() {
-    // No query here, move along please.
-  }
-
-  function render($values) {
-    $node = node_load($values->nid);
-    $gid = NULL;
-    if (in_array($node->type, (array)node_gallery_get_types('gallery'))) {
-      $gid = $node->nid;
-    }
-    elseif (in_array($node->type, (array)node_gallery_get_types('image'))) {
-      $gid = $node->gid;
-    }
-    $value = $gid ? node_gallery_get_image_count($gid) : '';
-
-    // The following code is copy/pasted from views_handler_field_numeric.inc
-    if (!empty($this->options['set_precision'])) {
-      $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
-    }
-    else {
-      $remainder = abs($value) - intval(abs($value));
-      $value = $value > 0 ? floor($value) : ceil($value);
-      $value = number_format($value, 0, '', $this->options['separator']);
-      if ($remainder) {
-        // The substr may not be locale safe.
-        $value .= $this->options['decimal'] . substr($remainder, 2);
-      }
-    }
-
-    // Check to see if hiding should happen before adding prefix and suffix.
-    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
-      return '';
-    }
-
-    return check_plain($this->options['prefix'] . $value . $this->options['suffix']);
-
-  }
-}
