Always use transactions when you add, change, or delete a set of records using code. Because transactions buffer disk writes until they are committed, using a transaction reduces disk access to roughly once per transaction instead of once per record. This can result in dramatic improvements in performance.
It's a good idea to group data updates into logical sets of operations and then perform each set in a transaction; don't try to include all updates in one large transaction. Most operations inside a transaction are buffered in memory and then written to disk when the transaction is committed. If the transaction is too large, it uses memory that other operations may need; if it's too small, it accesses the disk more often than necessary.