Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Which is faster - Append or script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Which is faster - Append or script

    I have to append about 1200 records to a table. I'm currently doing it using the append operation.

    Would it run faster if I opened one table and added them to another using code like the following.

    Code:
    Tbl = table.open("Meds.dbf")
    query.order = "" 
    query.filter = "IF(var->v_Location='All Locations',.T.,Location=var->v_Location).and.NC='Med'.and.clientid<>''.and.(Date_dc={}.or.date_dc>var->v_BeginDate)" 
    query.options = "I"
    tbl.query_create()
    
    tbl.fetch_first()
    
    t2 = table.open("Med_Adm.dbf")
    
    tbl.batch_begin()
    t2.batch_begin()
    
    While .not. tbl.fetch_eof()
    	
    	t2.enter_begin()
    	    t2.medid = tbl.medid
    	    t2.time = tbl.time
    	    t2.pteid = tbl.clientid
                ... about 20 more fields	
            t2.enter_end(.T.)
    		 
    tbl.fetch_next() 
    
    end while 
    t2.batch_end()
    tbl.batch_end()
    
    t2.close()
    tbl.close(
    I know I could try it and see, but I hate to begin adding 30+ fields without some advice. Maybe someone will save me some time, which would be much appreciated.

    Thanks,

    Tom

    #2
    Re: Which is faster - Append or script

    Someone will probably have a definitive answer but in my experience the xbasic for an operation is faster than coding as you have done. I would suggest you capture the xbasic of the append operation and modify the table.open() command to include the read/write exclusive parameter.
    There can be only one.

    Comment


      #3
      Re: Which is faster - Append or script

      I second what Stan said. And 1200 records should go very fast no matter what, i.e., that's not a lot of records.

      Raymond Lyons

      Comment


        #4
        Re: Which is faster - Append or script

        You don't mention if you have any indexes created for the fields used in the filter expression. If not, adding them and optimizing the expression should speed things up. I would think indexes on clientid and NC would help. Not certain of how the if() responds to LQO but I think I would move the others forward in the expression


        query.filter = "NC='Med' .and. clientid<>'' .and. IF(var->v_Location='All Locations',.T.,Location=var->v_Location) .and. (Date_dc={} .or. date_dc>var->v_BeginDate)"
        There can be only one.

        Comment


          #5
          Re: Which is faster - Append or script

          Thanks for the suggestions.

          Stan - are you saying I should open the table in exclusive mode? e.g. table.Open("med_adm",FILE_RW_EXCLUSIVE) Might this speed it up?

          I did find and eliminate a field rule that filled using a calc expression and Lookup function.

          I'll work harder getting the current operation to work more quickly.

          Thanks,

          Tom

          Comment


            #6
            Re: Which is faster - Append or script

            are you saying I should open the table in exclusive mode?
            If you are running the xbasic of an append operation and it will not interfere with another's use of the table you should use table.Open("med_adm",FILE_RW_EXCLUSIVE).

            If the FILE_RW_EXCLUSIVE mode is obtained Alpha doesn't waste any time checking to see if another process wants to access the table.
            There can be only one.

            Comment


              #7
              Re: Which is faster - Append or script

              WOW - I just completed an append operation that took 20 minutes last month in less than 15 seconds!!!

              Thanks Again,

              Tom

              Comment


                #8
                Re: Which is faster - Append or script

                So call your wife and tell her you're taking her out to dinner to celebrate.
                There can be only one.

                Comment


                  #9
                  Re: Which is faster - Append or script

                  You don't have to write out 20 fields. AHHHHH, the beauty of evaluate_template()!!!

                  Code:
                  ' get the table fields into a list for entry
                  	dim vc_table_fields as C
                  	vc_table_fields=table.external_field_name_get("Med_Adm","N")
                  
                  'open the source and target tables, filtering the source table
                  	dim tt as p
                  	dim ts as p
                  	tt = table.open("Med_Adm.dbf")
                  
                  	dim ts_filter as C
                  	ts_filter="IF(var->v_Location='All Locations',.T.,Location=var->v_Location).and.NC='Med'.and.clientid<>''.and.(Date_dc={}.or.date_dc>var->v_BeginDate)" 
                  
                  	ts = table.open("Meds.dbf")
                  	xs=ts.order("",ts_filter)
                  	ts.batch_begin()
                  	tbl.fetch_first()
                  	while .not. ts.fetch_next()
                  		for each foo in vc_table_fields
                  			tt.enter_begin()
                  			evaluate_template("tt."+alltrim(foo)+"="+"ts."+alltrim(foo))
                  			tt.enter_end(.t.)
                  		next
                  	ts.fetch_next() 
                  	end while 
                  	ts.batch_end()
                  	ts.close()
                  	tt.close()
                  Mike W
                  __________________________
                  "I rebel in at least small things to express to the world that I have not completely surrendered"

                  Comment


                    #10
                    Re: Which is faster - Append or script

                    Mike,

                    Jeeeeeeeeeeeez, but that's clever. I'll have to study it closer.

                    Thanks Much,

                    Tom

                    Comment


                      #11
                      Re: Which is faster - Append or script

                      You can do the same with the xbasic of the append script. The really nice part is if you alter the structure of the tables involved, so long as you change both of them to match, the script rewrites itself. You don't need to edit it.
                      There can be only one.

                      Comment


                        #12
                        Re: Which is faster - Append or script

                        Hey Mike,

                        Is the following code right?

                        Code:
                        tbl.fetch_first()
                        	while .not. ts.fetch_next()
                        		for each foo in vc_table_fields
                        			tt.enter_begin()
                        			evaluate_template("tt."+alltrim(foo)+"="+"ts."+alltrim(foo))
                        			tt.enter_end(.t.)
                        		next
                        1) the "tbl" pointer is defined where ?

                        2) will a new record be started in the target table for each field in the source ?

                        3) will the "fetch_next()" method of the table object return a logical value that can be used to terminate the while / end while loop ?

                        I must be missing something.

                        Comment


                          #13
                          Re: Which is faster - Append or script

                          Here's the way I do it. Script predicated on matching fields in both tables in the same order as seen in the table structure editor (type, width, and decimals also matching). The field names in either table can be changed and the append still works. Add a field(s) of any name to both tables with matching data specifications and the append still works.

                          Sample tables attached.

                          Code:
                          mast_flds = table.external_field_name_get("appendto","N")
                          mast_count = line_count(mast_flds)
                          trans_flds = table.external_field_name_get("appendfrom","N")
                          trans_count = line_count(trans_flds)
                          if mast_count <> trans_count
                          	ui_msg_box("Warning","Field counts are not the same.")
                          	end
                          end if
                          
                          DIM Append as P
                          a_tbl = table.open("appendto",FILE_RW_EXCLUSIVE)
                          append.t_db = "appendfrom"
                          ON ERROR GOTO ERROR3105201314440284
                          append.m_key = ""
                          append.t_key = ""
                          append.m_filter = ""
                          append.t_filter = ""
                          append.type = "All"
                          append.m_count = mast_count
                          for qx = 1 to mast_count
                          eval("append.m_field"+alltrim(str(qx))) = word(mast_flds,qx)
                          eval("append.m_exp"+alltrim(str(qx))) = "@APPENDFROM->"+word(trans_flds,qx)
                          next qx
                          append.t_count = 0
                          a_tbl.append()
                          
                          GOTO CONTINUE3105201314440284
                          ERROR3105201314440284:
                          ON ERROR GOTO 0
                          ui_msg_box("Error","Error running Append Operation"+crlf()+error_text_get())
                          END
                          CONTINUE3105201314440284:
                          a_tbl.close()
                          fromandto.zip
                          There can be only one.

                          Comment


                            #14
                            Re: Which is faster - Append or script

                            If they have matching structure, nothing comes close to the speed of .Copy_Records_To(
                            especially as opposed to append
                            15000 large records (70 fields) in 2.8 secs on a not very fast notebook. Indices intact.

                            Comment


                              #15
                              Re: Which is faster - Append or script

                              Tom,
                              of course you are correct. A couple of lines out of place and typo. For completeness..
                              Code:
                              ' get the table fields into a list for entry
                              	dim vc_table_fields as C
                              	vc_table_fields=table.external_field_name_get("Med_Adm","N")
                              
                              'open the source and target tables, filtering the source table
                              	dim tt as p
                              	dim ts as p
                              	tt = table.open("Med_Adm.dbf")
                              
                              	dim ts_filter as C
                              	ts_filter="IF(var->v_Location='All Locations',.T.,Location=var->v_Location).and.NC='Med'.and.clientid<>''.and.(Date_dc={}.or.date_dc>var->v_BeginDate)" 
                              
                              	[COLOR="#0000FF"]'open the source table filtered[/COLOR]
                              	ts = table.open("Meds.dbf")
                              	xs=ts.order("",ts_filter)
                              	ts.batch_begin()
                              	[COLOR="#FF0000"]ts[/COLOR].fetch_first()
                              
                              	[COLOR="#0000FF"]'loop through the source table records to copy to target table[/COLOR]
                              	while .not. ts.fetch_[COLOR="#FF0000"]eof[/COLOR]()
                              
                              		'[COLOR="#0000FF"]begin a new record in the target table[/COLOR]
                              		tt.enter_begin()
                              	
                              			[COLOR="#0000FF"]'loop thru each field and make an entry from source table field to target table field[/COLOR]
                              			for each foo in vc_table_fields
                              				evaluate_template("tt."+alltrim(foo)+"="+"ts."+alltrim(foo))
                              			next
                              
                              		[COLOR="#0000FF"]'save the target record[/COLOR]
                              		tt.enter_end(.t.)
                              
                              	[COLOR="#0000FF"]'fetch a new source table record to copy to target table[/COLOR]
                              	ts.fetch_next() 
                              	end while 
                              	ts.batch_end()
                              	ts.close()
                              	tt.close()
                              Mike W
                              __________________________
                              "I rebel in at least small things to express to the world that I have not completely surrendered"

                              Comment

                              Working...
                              X