简体   繁体   中英

MySQL - Why does a select statement on an indexed column return the index information, and not the column data?

I have a table (InnoDB) that stores an activity log. Each row has a user id, an activity id, and location data. The user id and the activity id columns are indexed, though not unique.

When I query select * the data returned looks normal -

select * from table

When I query user id or activity id with another column (even those 2 together), the data looks normal.

select user_id, city from table

When I query for just the user id or activity id with a where statement, the data looks normal.

select user_id from table where city = 'boulder'

The issue arises when I query for just the user id or activity id alone, or query just one with a where statement on the same column with a substring query.

select user_id from table

or

select user_id from table where substring(user_id, 1,5) = '12345'

The data returned is not the data in the field, but what looks like the index location (or something similar). I dropped the indexes, and the problem was fixed, but reappeared as soon as I added the indexes back.

Example without index -

user_id   
-------
123456789
231234567
234567543

Example with index -

user_id
-------
081357652234
100000000000000
1000000000011

I've tried restarting the server, and then reloading the data from the api, but nothing helped. I've tested this on other tables in the same database without experiencing this problem.

Is this a bug or some mistake I made in configuration?

I would:

  • change the name of the table (i see you updated it to user_activity) to avoid possible bugs due to table name being also a keyword in sql
  • make a backup of the db
  • run a check table user_activity for errors
  • check that user_id is an int type

UPDATE : As i can see from one of your comments that user_id is a varchar, you may have run into some bug because the second and third id returned by your query look like binary. As you're working with InnoDB you can't run REPAIR but you can look at this . It may also be a good idea to dump the db and reload it to see if your problem is circumstantial or consistently happening no matter what.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM