Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:31 AM
=?ISO-8859-2?Q?Tomasz_Sok=F3lski?=
 
Posts: n/a
Default Problem with very long 'create table as select' query

This query has at about 19kB:

create tab_out as
select
x1*(0.019613279029726982)+x2*(0.018225347623229027 )+...+x1000*(0.037470344454050064)
as zm1,
from tab_in

After running it database server crashes and after auto restart I have
in log:

061106 11:42:12 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.22-max-log' socket: '/var/run/mysql/mysql.sock' port:
3306 MySQL Community Edition - Experimental (GPL)

Number of processes running now: 0
061106 15:56:03 mysqld restarted
061106 15:56:04 InnoDB: Started; log sequence number 0 43655
061106 15:56:04 [Note] Recovering after a crash using kamilajs-bin
061106 15:56:10 [Note] Starting crash recovery...
061106 15:56:10 [Note] Crash recovery finished.
061106 15:56:10 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.22-max-log' socket: '/var/run/mysql/mysql.sock' port:
3306 MySQL Community Edition - Experimental (GPL)

When I've changed this query to:

create tab_out as
select
x1*(0.019613279029726982)+x2*(0.018225347623229027 )+...+x500*(0.037470344454050064)
as zm1a,
x501*(0.019613279029726982)+x2*(0.0182253476232290 27)+...+x1000*(0.037470344454050064)
as zm1b,
from tab_in

everything is OK. Is there some kind of limit on variable definition
length? And if - why mysql crashes not telling me what is the problem?
How to fix it?

Thank you for any suggestions.

Best regards.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:32 AM
Axel Schwenke
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

=?ISO-8859-2?Q?Tomasz_Sok=F3lski?= <askman25@NOSPAMgazeta.pl> wrote:
> This query has at about 19kB:
>
> create tab_out as
> select
> x1*(0.019613279029726982)+x2*(0.018225347623229027 )+...+x1000*(0.037470344454050064)
> as zm1,
> from tab_in


Wait! You have a table with 1000 columns?
Now that's a weird design.

> After running it database server crashes and after auto restart I have
> in log:
>
> 061106 11:42:12 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
> Version: '5.0.22-max-log' socket: '/var/run/mysql/mysql.sock' port:
> 3306 MySQL Community Edition - Experimental (GPL)
>
> Number of processes running now: 0
> 061106 15:56:03 mysqld restarted


Well, there is nothing in the log.

> When I've changed this query to:
>
> create tab_out as
> select
> x1*(0.019613279029726982)+x2*(0.018225347623229027 )+...+x500*(0.037470344454050064)
> as zm1a,
> x501*(0.019613279029726982)+x2*(0.0182253476232290 27)+...+x1000*(0.037470344454050064)
> as zm1b,
> from tab_in
>
> everything is OK. Is there some kind of limit on variable definition
> length? And if - why mysql crashes not telling me what is the problem?
> How to fix it?


I just tried with a little Perl test program and a recent MySQL-5.0.
I created a table with 999 columns of type FLOAT and filled it with
100 rows of random data. Then I SELECTed

rand()*c001
rand()*c001 + rand()*c002
rand()*c001 + rand()*c002 + rand()*c003
....
rand()*c001 + rand()*c002 + rand()*c003 + ... + rand()*c999


for 536 columns I get the following error:

> DBD::mysql::st execute failed: Thread stack overrun:
> 186116 bytes used of a 196608 byte stack, and 10788 bytes needed.
> Use 'mysqld -O thread_stack=#' to specify a bigger stack.


Please note: this is a normal SQL error, reported from mysqld to the
client. Mysqld did not crash. I restarted mysqld with thread_stack=512K
in my.cnf and this time my test program finished without a problem.


Conclusion: the SQL parser eats memory if it has to parse long
expressions. You can compensate by configuring a bigger stack.


I cannot explain why your mysqld crashed. Maybe the stack isn't
checked in 5.0.22 or your specific build of it.


PS: I could not create a 1000 x FLOAT table in InnoDB because that
would have exceeded the maximum row length. So I used 999.

PPS: find my test program attached


XL
--
Axel Schwenke, Senior Software Developer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 08:32 AM
=?ISO-8859-1?Q?Tomasz_Sok=F3lski?=
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Axel Schwenke napisa?(a):
> Wait! You have a table with 1000 columns?
> Now that's a weird design.


That's not my design - I just work with problems with such designs
BTW - how to store data from forms - where each form has 1000 questions.
Each record is one filled form which contains 1000 answers.

>> DBD::mysql::st execute failed: Thread stack overrun:
>> 186116 bytes used of a 196608 byte stack, and 10788 bytes needed.
>> Use 'mysqld -O thread_stack=#' to specify a bigger stack.


Thank you very much - it helped.

> Please note: this is a normal SQL error, reported from mysqld to the
> client. Mysqld did not crash. I restarted mysqld with thread_stack=512K
> in my.cnf and this time my test program finished without a problem.
> Conclusion: the SQL parser eats memory if it has to parse long
> expressions. You can compensate by configuring a bigger stack.
> I cannot explain why your mysqld crashed. Maybe the stack isn't
> checked in 5.0.22 or your specific build of it.


Very weird - this is standard version from www.mysql.org running on
Slackware 10.2 with 2.6.17.7 kernel.

> PS: I could not create a 1000 x FLOAT table in InnoDB because that
> would have exceeded the maximum row length. So I used 999.


We use MyIsam engine.

> PPS: find my test program attached


Thank's a lot!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 08:32 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Tomasz Sokólski wrote:
> Axel Schwenke napisa?(a):
>
>> Wait! You have a table with 1000 columns?
>> Now that's a weird design.

>
>
> That's not my design - I just work with problems with such designs
> BTW - how to store data from forms - where each form has 1000 questions.
> Each record is one filled form which contains 1000 answers.
>


Read up on database normalization. A much better way would be to have a
second table with three columns - userid, questionid and answer.

>>> DBD::mysql::st execute failed: Thread stack overrun:
>>> 186116 bytes used of a 196608 byte stack, and 10788 bytes needed.
>>> Use 'mysqld -O thread_stack=#' to specify a bigger stack.

>
>
> Thank you very much - it helped.
>
>> Please note: this is a normal SQL error, reported from mysqld to the
>> client. Mysqld did not crash. I restarted mysqld with thread_stack=512K
>> in my.cnf and this time my test program finished without a problem.

>
> > Conclusion: the SQL parser eats memory if it has to parse long
> > expressions. You can compensate by configuring a bigger stack.
> > I cannot explain why your mysqld crashed. Maybe the stack isn't
> > checked in 5.0.22 or your specific build of it.

>
> Very weird - this is standard version from www.mysql.org running on
> Slackware 10.2 with 2.6.17.7 kernel.
>
>> PS: I could not create a 1000 x FLOAT table in InnoDB because that
>> would have exceeded the maximum row length. So I used 999.

>
>
> We use MyIsam engine.
>
>> PPS: find my test program attached

>
>
> Thank's a lot!



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 08:35 AM
=?UTF-8?B?VG9tYXN6IFNva8OzbHNraQ==?=
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Jerry Stuckle napisał(a):
>> That's not my design - I just work with problems with such designs
>> BTW - how to store data from forms - where each form has 1000
>> questions. Each record is one filled form which contains 1000 answers.

> Read up on database normalization. A much better way would be to have a
> second table with three columns - userid, questionid and answer.


Very interesting.

So we have table:

userid questionid answer

paul 1 0.33333
paul 2 0.45454
paul 3 0.23232
john 1 0.55555
john 2 0.44444
john 3 0.22222

I want to insert new records:

userid questionid answer

paul 4 answer=questionid[1].answer(where userid = paul) +
questionid[2].answer(where userid = paul)
john 4 answer=questionid[1].answer(where userid = john) +
questionid[2].answer(where userid = paul)

Is there a possibility to insert this records in one sql command?

(sorry - I'm not an SQL programmer - I would like to know if this is
hard to write in one sql command)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 08:35 AM
=?UTF-8?B?VG9tYXN6IFNva8OzbHNraQ==?=
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Tomasz Sokólski napisał(a):
> paul 4 answer=questionid[1].answer(where userid = paul) +
> questionid[2].answer(where userid = paul)
> john 4 answer=questionid[1].answer(where userid = john) +
> questionid[2].answer(where userid = paul)


I mean:

paul 4 answer=questionid[1].answer(where userid = paul) +
questionid[2].answer(where userid = paul)
john 4 answer=questionid[1].answer(where userid = john) +
questionid[2].answer(where userid = john)


If I have 1000 columns with answers it's simple - I can insert new
column question1001 = question1 + question2 - how to do that if I have just:

userid questionid answer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 08:35 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Tomasz Sokólski wrote:
> Jerry Stuckle napisał(a):
>
>>> That's not my design - I just work with problems with such designs
>>> BTW - how to store data from forms - where each form has 1000
>>> questions. Each record is one filled form which contains 1000 answers.

>>
>> Read up on database normalization. A much better way would be to have
>> a second table with three columns - userid, questionid and answer.

>
>
> Very interesting.
>
> So we have table:
>
> userid questionid answer
>
> paul 1 0.33333
> paul 2 0.45454
> paul 3 0.23232
> john 1 0.55555
> john 2 0.44444
> john 3 0.22222
>
> I want to insert new records:
>
> userid questionid answer
>
> paul 4 answer=questionid[1].answer(where userid = paul) +
> questionid[2].answer(where userid = paul)
> john 4 answer=questionid[1].answer(where userid = john) +
> questionid[2].answer(where userid = paul)
>
> Is there a possibility to insert this records in one sql command?
>
> (sorry - I'm not an SQL programmer - I would like to know if this is
> hard to write in one sql command)


Not a problem at all.

INSERT INTO answers (name, questionid, answer) VALUES ('paul',
questionid, answer);

Of course, you would also be better off using id's - for instance, what
if you have two "paul"s? Something like:


Table TestTakers
id name

Table Answers

id question answer

Your TestTakers table might look like:

1 paul
2 John

And your Answers table might look like

1 1 0.33333
1 2 0.45454
1 3 0.23232
2 1 0.55555
2 2 0.44444
2 3 0.22222

Then to get a specific answer you would join the two tables, i.e.

SELECT answer FROM Answers
JOIN TestTakers ON Answers.id = TestTakers.id
WHERE questionid = 2;

You really need to read up on SQL and database normalization. SQL is a
very powerful language, and proper normalization will make you life much
easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 08:35 AM
=?UTF-8?B?VG9tYXN6IFNva8OzbHNraQ==?=
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Jerry Stuckle napisał(a):
> Not a problem at all.
>
> INSERT INTO answers (name, questionid, answer) VALUES ('paul',
> questionid, answer);
>


No - I know how to INSERT rows and how to join tables - but the problem is:

We have one table - for example:

userid variableid variablevalue
1 1 0.3
1 2 0.2
1 3 0.6
2 1 0.3
2 2 0.4
2 3 0.1

the problem is: For every user in the table (userid) find a value
(variablevalue) for new variable (variableid = 4) according to:

variable 4 = variable 2 + variable 3

and insert this new variable into table as new record - so the table
after operation would be:

userid variableid variablevalue
1 1 0.3
1 2 0.2
1 3 0.6
1 4 0.8 (0.2 + 0.6)
2 1 0.3
2 2 0.4
2 3 0.1
2 4 0.5 (0.4 + 0.1)

Is this possible with just one SQL command?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 08:35 AM
Axel Schwenke
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

=?UTF-8?B?VG9tYXN6IFNva8OzbHNraQ==?= <askman25@NOSPAMgazeta.pl> wrote:
>
> I know how to INSERT rows and how to join tables - but the problem is:
>
> We have one table - for example:
>
> userid variableid variablevalue
> 1 1 0.3
> 1 2 0.2
> 1 3 0.6
> 2 1 0.3
> 2 2 0.4
> 2 3 0.1
>
> the problem is: For every user in the table (userid) find a value
> (variablevalue) for new variable (variableid = 4) according to:
>
> variable 4 = variable 2 + variable 3
>
> and insert this new variable into table as new record - so the table
> after operation would be:
>
> userid variableid variablevalue
> 1 1 0.3
> 1 2 0.2
> 1 3 0.6
> 1 4 0.8 (0.2 + 0.6)
> 2 1 0.3
> 2 2 0.4
> 2 3 0.1
> 2 4 0.5 (0.4 + 0.1)


Why would somebody like to do that? It's adding redundant data to the
table. If you need the sum of some answers per user, use GROUP BY:

SELECT userid, SUM(variablevalue) AS value FROM foo
WHERE variableid IN (2,3) GROUP BY userid

> Is this possible with just one SQL command?


You can use INSERT ... SELECT ... to select from the table and insert
the results at the same time. Again: this will denormalize your data.


XL
--
Axel Schwenke, Senior Software Developer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-28-2008, 08:35 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Tomasz Sokólski wrote:
> Jerry Stuckle napisał(a):
>
>> Not a problem at all.
>>
>> INSERT INTO answers (name, questionid, answer) VALUES ('paul',
>> questionid, answer);
>>

>
> No - I know how to INSERT rows and how to join tables - but the problem is:
>
> We have one table - for example:
>
> userid variableid variablevalue
> 1 1 0.3
> 1 2 0.2
> 1 3 0.6
> 2 1 0.3
> 2 2 0.4
> 2 3 0.1
>
> the problem is: For every user in the table (userid) find a value
> (variablevalue) for new variable (variableid = 4) according to:
>
> variable 4 = variable 2 + variable 3
>
> and insert this new variable into table as new record - so the table
> after operation would be:
>
> userid variableid variablevalue
> 1 1 0.3
> 1 2 0.2
> 1 3 0.6
> 1 4 0.8 (0.2 + 0.6)
> 2 1 0.3
> 2 2 0.4
> 2 3 0.1
> 2 4 0.5 (0.4 + 0.1)
>
> Is this possible with just one SQL command?



Tomasz,

I agree with Alex. This is adding redundant data to the table, and will
make it much harder to manage. What happens if you need to go in and
change variable 2, for instance? You'll have to recompute variable 4.

Rather, use the SQL calls to compute on the fly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:46 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526