Hiya,

I have a few views which reference the flag module and I was finding that whenever the query (this is on a site with 100k+ nodes of which only a few hundred have been flagged) was running, the query would take approximately 1.5 seconds. As a reference the query is something like:
SELECT node.nid AS nid, node.title AS node_title, node_data_field_photo_lossy_image.field_photo_lossy_image_fid AS node_data_field_photo_lossy_image_field_photo_lossy_image_fid, node_data_field_photo_lossy_image.field_photo_lossy_image_list AS node_data_field_photo_lossy_image_field_photo_lossy_image_list, node_data_field_photo_lossy_image.field_photo_lossy_image_data AS node_data_field_photo_lossy_image_field_photo_lossy_image_data, node.type AS node_type, node.vid AS node_vid, flag_content_node.timestamp AS flag_content_node_timestamp, flag_content_node_1.timestamp AS flag_content_node_1_timestamp, DATE_FORMAT((FROM_UNIXTIME(flag_content_node.timestamp) + INTERVAL -25200 SECOND), '%Y') AS flag_content_node_timestamp_year, DATE_FORMAT((FROM_UNIXTIME(flag_content_node_1.timestamp) + INTERVAL -25200 SECOND), '%Y') AS flag_content_node_1_timestamp_year, node.nid AS node_nid FROM node node LEFT JOIN content_type_calarts_photo node_data_field_photo_lossy_image ON node.vid = node_data_field_photo_lossy_image.vid INNER JOIN files files_node_data_field_photo_lossy_image ON node_data_field_photo_lossy_image.field_photo_lossy_image_fid = files_node_data_field_photo_lossy_image.fid LEFT JOIN flag_content flag_content_node ON node.nid = flag_content_node.content_id AND flag_content_node.fid = 4 LEFT JOIN flag_content flag_content_node_1 ON node.nid = flag_content_node_1.content_id AND flag_content_node_1.fid = 3 LEFT JOIN content_field_photo_gallery node_data_field_photo_gallery ON node.vid = node_data_field_photo_gallery.vid WHERE (node.status <> 0 OR (node.uid = 1 AND 1 <> 0) OR 1 = 1) AND (node_data_field_photo_gallery.field_photo_gallery_nid = 205385) ORDER BY flag_content_node_timestamp_year DESC, flag_content_node_1_timestamp_year DESC, node_title ASC, node_nid ASC LIMIT 0, 12
(Yes, I know the query looks quite nasty). Anyways, I was noticing that when I ran an explain on the query, it would be using the unique index 'fid_content_type_content_id_uid' but in that scenario the content_type is missing so the only index that is really being used in this scenario is fid which brings in a lot more rows.

To resolve this issue, I did a query CREATE INDEX fid_content_id ON flag_content(fid, content_id); and once I ran the explain, this index was being used and when I ran the query, the query time went down from 1.5 seconds to 0.05.

Even though I did this on D6 (and I'm currently using 1.3), I saw that the index on 1.3, 2.x for D6 and D7 were the same. So the patch I propose would be:
For D6:

  $return = array();
  db_add_index($return, 'flag_content', 'fid_content_id', array('fid', 'content_id'));

For D7:

  db_add_index('flag_content', 'fid_content_id', array('fid', 'content_id'));

Comments

quicksketch’s picture

Category: bug » feature

I'm really surprised this would be necessary. Why wouldn't the existing unique key be used?

From flag_schema():

    'unique keys' => array(
      'fid_content_id_uid_sid' => array('fid', 'content_id', 'uid', 'sid'),
    ),
btmash’s picture

Hmm, you're right. I had quickly glanced at the 6.x-2.x/7.x branches and not taken a look at the unique keys. That should work and thus what I wrote is unnecessary :/ My patch would really only be for the 6.x-1.x branch. I'm not sure if any patches are going into the 6.x-1.x branch at this point (and if they are not, is it wise to do an update to 6.x-2.x given that the project page says it is in beta? And if not, which issues would it be that are preventing the 2.x branch from coming out of beta? I'd be happy to try and help with those :)

quicksketch’s picture

Status: Active » Closed (fixed)

2.x is coming out of beta soonish (we hope). Now that we've got the integrations either upgraded or moved. We just had too many of them to adequately keep updated.

#1176902: Move Services integration into a separate module (previously Services 3 support)
#1030950: Drop Activity integration
#875366: Port the Rules integration to D7

I'm marking this issue fixed since it's already corrected in the 2.x branch.