473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating rows for insertion

geon
1 New Member
Hi!

I have a table I'm using as a stack to pop rows from. (The data in the stack is precomputed for efficiency and reliability.) There are a lot of duplicate rows in this table (hundreds or thousands of identical rows), but they are all inserted with a regular insert statement.


I have optimized it by using the multiple row insert syntax:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName (Col1, Col2) VALUES
  2. ("Some value", "Another value"),
  3. ("Some value", "Another value"),
  4. ("Some value", "Another value"),
  5. ("Some value", "Another value"),
  6.  
That helps a lot, but it's still terribly slow. (Nothe that there might be hundreds of identical rows inserted like this.)

I was thinking of mowing all this to the server insted, like this:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName SELECT "Some Value" AS Col1, "Another value" AS Col2
But I would need some way of reliably generating n rows from the select statement. I saw an example doing it this way:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName SELECT "Some Value" AS Col1, "Another value" AS Col2 FROM AnotherTableName LIMIT n
Where "n" would be the number of rows to insert. But what if there are no rows in the second table?

Any ideas?

(This is all going to be running on a webserver I do not have control over. Probably a FreeBSD/Linux solution with Apache/MySQL/PHP.)
Oct 12 '07 #1
1 1606
mwasif
802 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName SELECT "Some Value" AS Col1, "Another value" AS Col2 FROM AnotherTableName LIMIT n
Where "n" would be the number of rows to insert. But what if there are no rows in the second table?
'n' is the number of rows to SELECT. If there are no rows, it is not going to INSERT anything.

You can use this statment to INSERT the data in pieces e.g. INSERT first 100 rows
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName SELECT "Some Value" AS Col1, "Another value" AS Col2 FROM AnotherTableName LIMIT 0,1000
Then next 1000
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TableName SELECT "Some Value" AS Col1, "Another value" AS Col2 FROM AnotherTableName LIMIT 1000,2000
Nov 6 '07 #2

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

Similar topics

13
3788
by: J. Campbell | last post by:
I'm wanting to output a text header file in front of the data portion of an output file. However when I use "\n" or "endl" as end of line, it just puts a 0x0a in the file(which is non-printing)...in Windows you need the (0x0d 0x0a) pair, if you want a new-line in a text editor. What's the correct way to get this 2-byte sequence into a file? Thanks #include <iostream>
0
1319
by: Murad Nayal | last post by:
Hello, I vaguely remember reading in the manual that the order of the retrieved rows in a response to a select statement is unpredictable (unless you use an order by clause). this possibly depends on the indices set up for the table and/or used in constructing the result etc. is this accurate? if so is there any way to insure that rows retrieved are returning in the order by were inserted in, say other than ordering by some 'insertion...
4
3999
by: Mark | last post by:
BEGINNER QUESTION I have a table which has a compound primary key consisting of two columns. One of these columns is a foreign key which is generated in another table by an identity. I want to be able to generate the other primary key column value automatically when an insert occurs but assume that I cannot use an identity because it would have to be unique for this table.
4
1129
by: s99999999s2003 | last post by:
hi i wish to generate a table using cgi toprint = print '''<table border="1"> <tr> <td>User</td> <td>Name</td> <td>Address</td> </tr> <tr>
9
5010
by: YONETANI Tomokazu | last post by:
Hi. You can use the following SQL to construct rows with column names on the fly, rather than from an existing table like sysibm.sysdummy1: SELECT * FROM TABLE ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) X(foo, bar, baz); or WITH X(foo, bar, baz) AS ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) SELECT * FROM X;
5
1972
by: Henry | last post by:
Dear all, Is there any DB2 SQL statement that could generate rows of intermediate dates by providing the start and end dates?? If 2006-1-1 and 2006-1-31 are provided to the SQL, the output will look like : 2006-1-1 2006-1-2
7
3676
by: tatata9999 | last post by:
Hi, SQL env: sql server 2000 Target column of insertion: varchar(15) Case, a var is made up of a character of the following characters (random selection): A,B,C,D,E,$,!,%,^,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,&,*,(,) and a few numbers (random selection), and then
7
4722
by: Don Li | last post by:
Hi, Env is ms sql server 2000. ddl: create table srchPool(tid int primary key, taid int, s tynyint, uid tynyint); -- and sql server automatically creates a clustered index for the pk dml:
10
18408
sumittyagi
by: sumittyagi | last post by:
Hi All, Is it possible in oracle to insert multiple rows in a single insert query, as is possible in sql server: insert into test_table values ('abc', 'xyz'), ('mno', 'pqr'); This syntax is not supported in oracle. Is oracle having any alternative for this one. Thanks & happy new year in advance.
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.