Hi,

I found that VotingAPI is still using old db_query() functions. But that is not cool! It is keeps developers from using all power of PDO queries that was implemented in Drupal 7 a long time ago.

So I spent some time and converted all db_query() functions to db_select(). Patch attached.

Comments

andypost’s picture

Status: Needs review » Needs work
+++ b/API.txtundefined
@@ -80,20 +80,23 @@ very, very inefficient -- the slowdown of possibly running multiple aggregate qu
-function mymodule_votingapi_results_alter(&$results, $entity_type, $entity_id) {
+function hook_votingapi_results_alter(&$vote_results, $content_type, $content_id) {

please do not change mymodule here

+++ b/API.txtundefined
@@ -80,20 +80,23 @@ very, very inefficient -- the slowdown of possibly running multiple aggregate qu
+  $query->fields('v', array('tag'));
+  $query->addExpression('STDDEV(v.value)', 'standard_deviation');
+  $query->condition('v.content_type', $content_type);
+  $query->condition('v.content_id', $content_id);
+  $query->condition('v.value_type', 'percent');
+  $query->groupBy('v.tag');

+++ b/votingapi.api.phpundefined
@@ -31,21 +31,22 @@
+  $query->fields('v', array('tag'));
+  $query->addExpression('STDDEV(v.value)', 'standard_deviation');
+  $query->condition('v.content_type', $content_type);
+  $query->condition('v.content_id', $content_id);
+  $query->condition('v.value_type', 'percent');
+  $query->groupBy('v.tag');

+++ b/votingapi.installundefined
@@ -82,11 +82,16 @@ function votingapi_update_7202() {
+      $query->fields('v', array('entity_type', 'entity_id'));
+      $query->leftJoin($type_table, 'e', "e.$type_key = v.entity_id");
+      $query->condition('v.entity_type', $type);
+      $query->isNull("e.$type_key");
+      $query->groupBy('v.entity_type');
+      $query->groupBy('v.entity_id');

+++ b/votingapi.moduleundefined
@@ -525,12 +537,16 @@ function votingapi_metadata($reset = FALSE) {
+  $query->fields('v', array('value_type', 'tag'));
+  $query->addExpression('SUM(v.value)', 'value_sum');
+  $query->addExpression('COUNT(v.value)', 'value_count');
+  $query->condition('v.entity_type', $entity_type);
+  $query->condition('v.entity_id', $entity_id);
+  $query->condition('v.value_type', array('points', 'percent'));
+  $query->groupBy('v.value_type');
+  $query->groupBy('v.tag');

@@ -540,11 +556,16 @@ function votingapi_votingapi_storage_standard_results($entity_type, $entity_id)
+  $query->fields('v', array('tag', 'value', 'value_type'));
+  $query->addExpression('COUNT(1)', 'score');
+  $query->condition('v.entity_type', $entity_type);
+  $query->condition('v.entity_id', $entity_id);
+  $query->condition('v.value_type', array('option'));
+  $query->groupBy('v.value');
+  $query->groupBy('v.tag');
+  $query->groupBy('v.value_type');

no reason to prefix $query each line - use chaning

spleshka’s picture

Status: Needs work » Needs review
StatusFileSize
new8.95 KB

Thanks @andy, new patch attached.

spleshka’s picture

Fixed lines indents.

eaton’s picture

I found that VotingAPI is still using old db_query() functions. But that is not cool! It is keeps developers from using all power of PDO queries that was implemented in Drupal 7 a long time ago.

At least as of the code that I wrote, the use of db_query for SELECTs was quite deliberate! the PDO DBTNG query builder is much slower for simple selects, and unless the query is extremely dynamic (as with the generic aggregation query that DOES use PDO), there isn't much to be gained in code style, either.

Larry Garfield, one of the DBTNG architects, weighs in on the issue with VotingAPI used as a specific example in this blog post.

I'm not opposed to the conversion on philosophical grounds, but given the potential performance implications of one of the more speed-sensitive parts of VotingAPI, it'd be a shame to do it without thinking through the implications.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Now it looks nicer

EDIT I think this queries a much more expensive so impact of their conversion to db_select() is less evil. But readablity and alterability could be useful

spleshka’s picture

What about extending this queries by adding some tags to them? It will make this queries more flexible for other developers. I mean if we provide a new feature that uses power of PDO, will it be a good reason for convertsion of old queries?

spleshka’s picture

Status: Reviewed & tested by the community » Needs review

Agree with @andypost: conversion could lead to loss of 1ms for two main queries, but that queries are much more expensive than 1ms.

spleshka’s picture

By the way, speaking about expensive recalculate operations. We could use here same trick as User Points module does: when new points should be added, they do not recalculate all single points. Instead of that they simply add or remove certain amount of points from table with cached results. Could we implement similar functionality into VotingAPI? it will bring a great performance boost. I understand that this is a separate issue, but I should know if you like this idea.

spleshka’s picture

Status: Needs review » Reviewed & tested by the community

Oh, I missed that I've changed issue status. Return the previous value.

torotil’s picture

I don't think that performance is an issue here. But it still boils down to the questions:

  1. Does it make the code more readable? - I don't think so.
  2. Does it add features that are desired? - For the while I prefer higher level customization functions like the pluggable storage API. Is there actually a use case that requires this?

@Spleshka: I'm currently pondering an object oriented API for results that could also support incremental calculations. This would also have the advantage that you don't need to store the individual votes except to limit the number of times someone can vote. But this is a completely different issue.

I'm leaving this open for the while. But unless someone provides an otherwise not possible use-case this is a wontfix for me.

torotil’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Status: Reviewed & tested by the community » Closed (won't fix)
torotil’s picture

Issue summary: View changes

Fixed my English.