Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:07:46+00:00 2026-06-06T19:07:46+00:00

I’m using the following query to extract the frequent short values from a mediumblob

  • 0

I’m using the following query to extract the frequent short values from a mediumblob column :

select bytes, count(*) as n
from pr_value
where bytes is not null && length(bytes)<11 and variable_id=5783
group by bytes order by n desc limit 10;

The problem I have is that this query takes too much time (about 10 seconds with less than 1 million records) :

mysql> select bytes, count(*) as n from pr_value where bytes is not null && length(bytes)<11 and variable_id=5783 group by bytes order by n desc limit 10;
+-------+----+
| bytes | n  |
+-------+----+
| 32    | 21 |
| 27    | 20 |
| 52    | 20 |
| 23    | 19 |
| 25    | 19 |
| 26    | 19 |
| 28    | 19 |
| 29    | 19 |
| 30    | 19 |
| 31    | 19 |
+-------+----+

The table is as follows (unrelated columns not shown) :

mysql> describe pr_value;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| product_id  | int(11)       | NO   | PRI | NULL    |       |
| variable_id | int(11)       | NO   | PRI | NULL    |       |
| author_id   | int(11)       | NO   | PRI | NULL    |       |
| bytes       | mediumblob    | YES  | MUL | NULL    |       |
+-------------+---------------+------+-----+---------+-------+

The type is mediumblob because most values are big. Less than 10% are short as the ones I’m looking for with this specific query.

I have the following indexes :

mysql> show index from pr_value;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| pr_value |          0 | PRIMARY  |            1 | product_id  | A         |        8961 |     NULL | NULL   |      | BTREE      |         |               |
| pr_value |          0 | PRIMARY  |            2 | variable_id | A         |      842402 |     NULL | NULL   |      | BTREE      |         |               |
| pr_value |          0 | PRIMARY  |            3 | author_id   | A         |      842402 |     NULL | NULL   |      | BTREE      |         |               |
| pr_value |          1 | bytes    |            1 | bytes       | A         |      842402 |       10 | NULL   | YES  | BTREE      |         |               |
| pr_value |          1 | bytes    |            2 | variable_id | A         |      842402 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

MySQL explains my query like this :

mysql> explain select bytes, count(*) as n from pr_value where bytes is not null && length(bytes)<11 and variable_id=5783 group by bytes order by n desc limit 10; 
+----+-------------+----------+-------+---------------+-------+---------+------+--------+----------------------------------------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows   | Extra                                        |
+----+-------------+----------+-------+---------------+-------+---------+------+--------+----------------------------------------------+
|  1 | SIMPLE      | pr_value | range | bytes         | bytes | 13      | NULL | 421201 | Using where; Using temporary; Using filesort |
+----+-------------+----------+-------+---------------+-------+---------+------+--------+----------------------------------------------+

Note that the condition on the length of the bytes column can be removed without changing the duration.

What can I do to make this query fast ?

Of course i’d prefer not to have to add columns.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:07:47+00:00Added an answer on June 6, 2026 at 7:07 pm

    your index on (bytes, variable_id) is not very smart. If you always have a variable_id clause in your queries you should add index with variable_id first :

    (variable_id, bytes)

    It depend on how discriminant variable_id is. But it sould help.

    Another tip is to add a new indexed column with the result of “length(bytes)<11” :

    update pr_value set small = length(bytes)<11;
    

    Add a new index with (small,variable_id).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.