473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A query that is on a mission to turn my hair gray

dlite922
1,584 Expert 1GB
All I want to ask is

What can you think of in your WILDEST imagination that explains a query that returns results 1 out of several times?

The same exact query (from a mysql prompt) I execute (by pressing the UP key) repeatedly sometimes returns results other times it's an empty result set.

The data is not being rebuild and updated in the background. In fact there is a cron job that updates it every half hour, I've taken that out. There is no other process changing the data.

What in the Universe is going on?

The query is simple. SELECT some fields FROM a few different tables WHERE cartesian joins. (tried LEFT JOINS too).

I use a Stored Function that converts an ID between two format to join a couple of those tables (I had a thread about this function a while back if you remember)

Shoot whatever ideas you have at me. At this point what I know of simple logic is going out the window and I'm ready to smash the computer with a rock.

I envy cavemen


Thanks to anyone who can make me sane again,




Dan
Oct 1 '09 #1
2 1707
dlite922
1,584 Expert 1GB
I think I found a MySQL bug.

I'm going to create a sample DB with sample Tables and create a Stored Function.

I think the stored function caches the last value returned and returns it even if given a different input but only when used in a LEFT JOIN. Function works fine if you just do a SELECT with it.

Will keep you updated...



Dan
Oct 2 '09 #2
dlite922
1,584 Expert 1GB
Hey Guys,

If anybody has a test MySQL server handy, could you look over this bug report. I'll submit it to MySQL bug if you guys confirm it on your end too.

Thanks alot.

Expand|Select|Wrap|Line Numbers
  1.  
  2. /**
  3.  
  4. Desc:             Example Schema, Data and Query that illustrate a possible MySQL bug involving use Stored Function in a LEFT JOIN. 
  5.  
  6. Created:         10/05/2009
  7. Author:         Dan M (dlite922)
  8. Version:         mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (x86_64) using readline 5.0
  9. Repeatability:     Always 
  10.  
  11. /*
  12.  
  13. /** SCHEMA **/
  14. drop table IF EXISTS image;
  15. drop table IF EXISTS event_image;
  16. drop table IF EXISTS event;
  17.  
  18. Create table event (
  19.     event_id Int NOT NULL AUTO_INCREMENT,
  20.     preconverted_id Varchar(5) NOT NULL,
  21.     name Varchar(20) NOT NULL,
  22.     UNIQUE (event_id),
  23.  Primary Key (event_id)) ENGINE = MyISAM;
  24.  
  25. Create table event_image (
  26.     postconverted_id Int NOT NULL,
  27.     image_id Int,
  28.     UNIQUE (postconverted_id),
  29.  Primary Key (postconverted_id)) ENGINE = MyISAM;
  30.  
  31. Create table image (
  32.     image_id Int NOT NULL AUTO_INCREMENT,
  33.     image_name Varchar(20) NOT NULL,
  34.     image_type Varchar(4) NOT NULL,
  35.     UNIQUE (image_id),
  36.  Primary Key (image_id)) ENGINE = MyISAM;
  37.  
  38.  
  39.  
  40.  
  41. /** STORED FUNCTION TO CONVERT IDs **/ 
  42. DELIMITER $$
  43. DROP FUNCTION IF EXISTS ConvertID$$
  44. CREATE FUNCTION ConvertID(preconverted_id VARCHAR(5)) RETURNS INT
  45. DETERMINISTIC
  46. BEGIN
  47.  DECLARE catText CHAR(1); 
  48.  DECLARE catNum INT; 
  49.  DECLARE series CHAR(4); 
  50.  DECLARE FinalID INT;  
  51.  
  52.  SET catText = SUBSTRING(preconverted_id FROM 1 FOR 1); 
  53.  SET series = SUBSTRING(preconverted_id FROM 2 FOR 5); 
  54.  
  55.  IF(catText = 'A') THEN
  56.   SET catNum = 1;
  57.  ELSEIF(catText = 'B') THEN
  58.   SET catNum = 2;
  59.  ELSEIF(catText = 'C') THEN
  60.   SET catNum = 3;
  61.  END IF;
  62.  
  63.  SET FinalID = (catNum << 26 | series); 
  64.  RETURN FinalID;
  65. END $$
  66. DELIMITER ;
  67.  
  68.  
  69.  
  70.  
  71. /** SAMPLE DATA **/
  72. INSERT INTO event VALUES (1,'A1234','Bar'),(2,'B5678','Foo'),(3,'C9011','Baz'); 
  73. INSERT INTO event_image VALUES (67110098,55),(134223406,97),(201335603,42); 
  74. INSERT INTO image VALUES (55,'Bar_and_friends','jpg'),(97,'FoorAreYou','gif'),(42,'BazFishing','png'); 
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /** QUERY THAT EXPOSES BUG **/
  81. /** PLEASE OBSERVE: covertedColumn. All fields have the converted of the last ID (that of 'Baz'). **/
  82. SELECT 
  83.  e.event_id,
  84.  e.preconverted_id,
  85.  e.name, 
  86.  ConvertID(e.preconverted_id) AS convertedColumn,
  87.  ei.postconverted_id,
  88.  ei.image_id, 
  89.  i.image_id,
  90.  i.image_name,
  91.  i.image_type
  92. FROM 
  93.  event AS e LEFT JOIN event_image AS ei ON ConvertID(e.preconverted_id) = ei.postconverted_id 
  94.  LEFT JOIN image AS i ON ei.image_id = i.image_id; 
  95.  
  96.  
  97. /** If you don't believe my function is working properly checkout these results **/
  98. SELECT e.event_id, e.name, ConvertID(e.preconverted_id) FROM event AS e;
  99.  
  100. // OR SIMPLY: 
  101. SELECT ConvertID('A1234'); 
  102. SELECT ConvertID('B5678'); 
  103. SELECT ConvertID('C9011'); 
  104.  
  105.  
  106. // END OF FILE 
  107.  
  108.  
I've tried with and without the DETERMINISTIC flag on the function.

Please let me know if you see something odd. I often make silly mistakes.

Thanks again,


Dan
Oct 5 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: RH | last post by:
<SELECT name="txtServices" size="3" multiple> <OPTION value="Massage">Massage</OPTION> <OPTION value="Facials">Facials</OPTION> <OPTION value="Wet & Dry Body Treatments">Wet &amp; Dry Body...
2
by: Tony | last post by:
Question... I am preparing a presentation on Oracle for my Senior Capstone class at Colorado State University. Does ANYONE know Oracle's Mission Statement or have a link pointing to it...
0
by: longhorntx | last post by:
I have a table with following values City STATE COLOR REGION city1 Texas White SW city2 Texas Gray SW city3 Texas Gray SW city4 Nevada Gray PAC c5 Texas Yellow SW c6 Florida Yellow SE...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
3
by: John | last post by:
Is there a way to code the button that's available in the query window--microsoft excel icon that exports to excel. I know transferspreadsheet will do this---but I want the query, which is in a...
2
by: Alan | last post by:
I'm having a bit of difficulty getting the results I need from our database. In a nutshell I'm trying to work out trends in what people buy next. So, for example, I'm trying to run a query that...
5
by: Photo | last post by:
Hello everyone this is my first post so as you could guess I need some help. Here is my situation, I have 2 tables, one caled personnel and one called DET_Dates, DET_Dates is linked to Personnel on...
4
by: lorirobn | last post by:
Hi, I have a report displaying items that are missing from a room. I created 2 queries, the first getting the items IN the room, and the second being an "unmatched" query that references the...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.